mswjs / msw

Seamless REST/GraphQL API mocking library for browser and Node.js.
https://mswjs.io
MIT License
15.4k stars 487 forks source link

Response delay not working? #136

Closed mitchconquer closed 4 years ago

mitchconquer commented 4 years ago

Describe 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

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:

  1. Create CRA with --template typescript
  2. Add MSW with yarn add -D msw
  3. Run npx msw init ./public
  4. Create route with delay
  5. 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!

msw-delay

modularcoder commented 4 years ago

+1 happens for me

hehehai commented 4 years ago

@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

image

after modification

setTimeout(
    resolve.bind(this, createResponse(clientMessage)),
    clientMessage.payload.delay,
)

We'll fix it as soon as possible

modularcoder commented 4 years ago

@hehehai that worked, thanks a lot!

mitchconquer commented 4 years ago

Awesome, thanks!!

kettanaito commented 4 years ago

@hehehai, thank you for the fix! Indeed, since the message structure change I've overlooked where the delay is stored. Your changes look good, will approve and release.

Note that by design you shouldn't modify the mockServiceWorker.js directly. Once the patch is released, the library will suggest you to replace it with npx msw init once more, which is the established way to propagate changes and fixes like this one to the worker file.

kettanaito commented 4 years ago

Released in 0.15.5. Please update and run npx 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.