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.

Add delay to all requests

See original GitHub issue

Is your feature request related to a problem? Please describe. When using msw within the browser (either storybook or my app), I would like to add a delay to all the responses to get a more realistic behavior.

In tests, I would like the delay to be minimal, as it currently is.

Describe the solution you’d like One solution would be to support a default delay option when starting a worker:

worker.start({
  delay: 1000,
})

I can then configure the delay to be different for different environments.

I’m not sure how this would interact with ctx.delay, would they both be honored or would one override the other?

Describe alternatives you’ve considered I could add ctx.delay to every handler, but this feels like a lot of boilerplate for something I want applied to all handlers.

I tried adding a handler to catch all requests and add a delay, but the requests didn’t fall to the next handler as I expected. This might be related to https://github.com/mswjs/msw/issues/258.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
kettanaitocommented, Jul 22, 2020

Hey, @garhbod. There’s actually the very same usage example in the recently updated docs already: in the Custom response composition section. It illustrates how you can create your own res function that has a random realistic response delay baked in. I find this more educational and practical as well, and see little reason to extend public API for this purpose.

2reactions
garhbodcommented, Jul 22, 2020

I got here looking for the suggested change to the api as I would have thought it would be something standard but after seeing the solution above I would vote not to add it to the API and simply add the solution above to the documentation somewhere. It serves its purpose and is more educational than creating an global .delay()

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
Simulate Delays in HTTP Requests - tweak browser extension
2. Configure the request you want to delay​ · Request URL. · Select your HTTP method from the dropdown. · Add the response...
Read more >
Angular 5+ : How to put a delay to a / all HttpClient requests?
2. Implement an HttpInterceptor and add the delay there using setTimeout() . · 3. My opinion is that you shouldn't delay the actual...
Read more >
Applying different delays based on HTTP method
Let's apply a delay of 2 seconds on responses to GET requests only made to echo.jsontest.com/b/c . Run the following to create and...
Read more >
Add delay to a request in ExpressJS - An Idiosyncratic Blog
I have two setTimeout calls in the middleware. The first one is to delay processing the request by 2 seconds. The second one...
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