Response delay not working?
See original GitHub issueDescribe the bug
Hi! I’m using MSW with Create React App Typescript and I’m having an issue getting the response delay to work. The MSW log message in the console does respect the response delay but the fetch response does not.
Environment
msw: ^0.13.1
nodejs: v13.12.0
npm: 6.14.4
react-scripts: 3.4.1
typescript: ^3.8.3
- Chrome Version 81.0.4044.129 (Official Build) (64-bit)
To Reproduce
I have created a code sandbox to try to reproduce this issue but cannot get it to work. Not sure if you have any guidance on that?
Steps to reproduce the behavior:
- Create CRA with
--template typescript
- Add MSW with
yarn add -D msw
- Run
npx msw init ./public
- Create route with delay
- Fetch to route
When fetch to route, the response comes back immediately but the MSW console message waits for the delay and then appears.
Expected behavior
I expect the response from the mock API to wait until the delay before returning. Is that a misunderstanding of the library?
Screenshots
I have created a GIF to show the issue. I put a console log around my fetch statement like below to show when the request starts and finishes.
console.log(`Fetch request started: ${new Date()}`);
const res = await window.fetch(
`${API_URL_BASE}/${API_PREFIX}/${endpoint}`,
config
);
console.log(`Fetch response received: ${new Date()}`);
In the GIF, I click the “load more customers” button and you can see the fetch response start and finish immediately while the MSW log waits 2000ms to appear.
In my mocked endpoint, if I add the following lines, the fetch response is correctly delayed.
await new Promise((r) => {
setTimeout(r, faker.random.number(2000));
});
Any idea what could be causing this issue? Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top GitHub Comments
@mitchconquer Yes, it was a mistake.
This is because of the delay error in the mockserviceworker.js file. You can start by modifying the documentation in the mockserviceworker.js file
after modification
We’ll fix it as soon as possible
Released in
0.15.5
. Please update and runnpx msw init <PUBLIC_DIR>
once more for the changes in the worker file to propagate to your project automatically.Thank you once more for raising this and for a quick fix.