Is there a default delay applied to resonses? #553
-
Hi - thanks for making MSW. It's grand. We have been noticing a bit of lag for MSW to respond to requests. We have never used ctx.delay() Reading the docs on delay() it was not clear: Does MSW apply a default random delay to responses? Even when never calling delay()? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hi, @philals. Thanks for reaching out. MSW does not apply any default delay to mocked responses. You can see the default response properties here: Line 38 in b99be78 The random realistic response delay is applied when calling rest.get('/user', (req, res, ctx) => res(ctx.json(...))) // no delay
rest.post('/book', (req, res, ctx) => res(ctx.delay(), ctx.json(...))) // random realistic delay
rest.delete('/user', (req, res, ctx) => res.(ctx.delay(500), ctx.json(...))) // exact delay duration (500ms)
Does this happen when running MSW in a browser or in NodeJS? Or in both? Also, what kind of delay are you encountering (ms/s)? While there's no default delay set on the mocked response, the request/response lifecycle can still take time:
How to debug this?NodeJSIf you're experiencing this in NodeJS, try running your testing script with $ DEBUG=* npm test This prefix will output additional debugging information about what happens internally and how much time certain operations take. BrowserThere is no |
Beta Was this translation helpful? Give feedback.
-
Thanks for the detailed response. We figured the delay we are noticing was due to the points you outlined. These tests are React Testing Library running against a largish Create React App. So that would be these are running in NodeJS. Any delay in the browser is not really an issue for us. The main issue this is causing is in our tests. We notices a variable delay of up to 200ms in some cases. RTL's default timeout is 1000ms. So if you have 4 cascading requests on a page, then RTL would timeout. In some cases we are migrating from mock-fetch which resolves instantly. Resolutions for us were:
We'll look a little further into it if we start noticing things like this again. |
Beta Was this translation helpful? Give feedback.
-
PS Thanks for making MSW. We love the developer experience of writing tests and being able to use those handlers in browser! |
Beta Was this translation helpful? Give feedback.
Hi, @philals. Thanks for reaching out.
MSW does not apply any default delay to mocked responses. You can see the default response properties here:
msw/src/response.ts
Line 38 in b99be78
The random realistic response delay is applied when calling
ctx.delay()
without arguments when composing your mocked response. Like so:D…