Ability to pause response on runtime in the worker #1109
-
Mock service Worker is an awesome library, it's very helpful to mock the API response. See that the MSW has the delay() to add the delay in response. but here while testing we would like to have complete control over the delay in run time. For example:- if I want to test the state where there is a delay in response. then I have to set the it would be noce to have the runtime modifier to pause the response and resume as we want. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Hey, @rahulsinghk. Thanks for your kind words and for proposing this feature!
rest.get('/user', (req, res, ctx) => {
const url = new URL(location.href)
const delay = url.searchParams.get('delay') || 0
return res(ctx.delay(delay), ctx.text('hello world'))
})
Most test runners execute assertions immediately so even a 100ms delay is enough to test the loading state, for example. Do you have a concrete test case in mind you could share where this doesn't apply? Overall, having a pause/resume control over the network may be beneficial but I don't think it's what you need for testing in your particular case. If anybody needs this during development, you can utilize rest.get('/user', (req, res, ctx) => {
// The entire application will pause in the browser,
// allowing you to inspect its state.
debugger
}) |
Beta Was this translation helpful? Give feedback.
-
hi |
Beta Was this translation helpful? Give feedback.
-
Hey,
Now the issue with the delay is that we are delaying tests and make them longer then these need to be, because in most of cases, 3 seconds delay is more than enough and it could be quicker - maybe 1 second or even less would be enough, but on slower machines or during some hiccups (happens on the CI) the tests could fail if the delay is lower than 3 seconds because the actions take longer, so we have to keep it just in case as we are not 100% sure how long the in-between actions take, this is making the test longer than these need to be to reduce the flakiness. If there were an option to pause and resume requests then it would solve the issue, the tests would resume the request when needed (for example in 95% of cases it would only take 500ms of delay but in other 5% it would have to be higher), it would make it more reactive and make tests as fast as possible. This isn't a critical feature as the delay works fine, but for me having a pause/resume option would be fantastic for me as I would know the tests run as fast as possible and are reactive to changes. Let me know if you need some more explanation. |
Beta Was this translation helpful? Give feedback.
-
Hi, everyone! For those wondering how to pause/unpause a response with MSW, let me summarize and give a few suggestions below. BasicsThere's no concept of "pause" in HTTP responses because they are sent in one bulk. The exception being streams where you, indeed, can introduce an arbitrary pause between different streamed chunks. But let's talk about regular responses which is likely what you're mocking with MSW. Using delayThe most straightforward way to "pause" a response is to pend a response via the rest.get('/resource', (req, res, ctx) => {
// Delay this response by 3000ms.
return res(ctx.delay(3000), ctx.text('Hello world!'))
}) Combine this with runtime handlers (the Controlled delaysUsing In the example below, I will use DeferredPromise to delegate resolver resolution to the outer context (your actual test). This is how you do it: import { DeferredPromise } from '@open-draft/deferred-promise'
it('my test', () => {
const responsePromise = new DeferredPromise()
server.use(
rest.get('/resource', async (req, res, ctx) => {
await responsePromise
return res(ctx.text('Hello world!'))
})
)
// ...do some actions.
// This will resolve the promise, "unpausing" the response resolver
// that awaits it above. The mocked response will be sent immediately.
responsePromise.resolve()
// ...do some assertions.
}) If you want more control over the response resolver execution in tests, I highly recommend this deferred promise pattern. |
Beta Was this translation helpful? Give feedback.
Hi, everyone! For those wondering how to pause/unpause a response with MSW, let me summarize and give a few suggestions below.
Basics
There's no concept of "pause" in HTTP responses because they are sent in one bulk. The exception being streams where you, indeed, can introduce an arbitrary pause between different streamed chunks. But let's talk about regular responses which is likely what you're mocking with MSW.
Using delay
The most straightforward way to "pause" a response is to pend a response via the
ctx.delay()
function.Combine this with runtime h…