Add delay to all requests
See original GitHub issueIs 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:
- Created 3 years ago
- Reactions:4
- Comments:11 (10 by maintainers)
Top GitHub Comments
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.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()