question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Response delay not working?

See original GitHub issue

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

  • 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:

  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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
hehehaicommented, May 3, 2020

@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

1reaction
kettanaitocommented, May 3, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Delaying Intercept responses in Cypress - Stack Overflow
A fixed delay of 100ms should work and would not be noticeable in the test run. Yea, that is what I was hoping...
Read more >
delay() - Api - Mock Service Worker Docs
delay (). Delays the response by the given duration (in ms). When no duration is provided, uses a random realistic server response time....
Read more >
How to simulate the delay in a request with Cypress
In today's “Pinch of Cypress”, learn how to simulate a delay in an HTTP request to test this intermediate state of the application....
Read more >
Adding Delay to Network Requests
APIs like deelay.me and https://app.requestly.io/delay allow selectively delaying a network request. This kind of delay does not impact the browser.
Read more >
Simulating faults in API behavior - WireMock
A stub response can have a fixed delay attached to it, such that the response ... With the above settings the Hello world!...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found