Have delays changed between v0.36.8 and v1.3.5? #2535
-
Hi there 👋 We're in the process of upgrading from a very old version of MSW Apologies I don't have a full reproducible, but I'm hoping we don't necessarily need it as we I'm pretty much just wondering if you would know off the top of your head something that's changed internally in MSW or the interceptors packages between The test is something like this: it('disables button while adding user', async () => {
const user = userEvent.setup();
const submitButton = await screen.findByText('Add user');
expect(submitButton).not.toBeDisabled();
// This action will send a POST to /addUser
await user.click(submitButton);
// The button should be disabled while we wait.
// ❌ This fails as the test says "Received element is not disabled". Using "waitFor" also doesn't seem to work here.
expect(submitButton).toBeDisabled();
expect(await screen.findByText('User has been added')).toBeInTheDocument();
expect(submitButton).not.toBeDisabled(); We are loading the handler in our // addUserHandler.ts
rest.post(`/addUser`, (req, res, ctx) => {
return res(ctx.status(200), ctx.json({}));
}) Adding a delay in the handler does work, but I don't want this to slow down each test that uses this shared handler, just one particular test that checks for the disabled button. rest.post(`/addUser`, (req, res, ctx) => {
return res(ctx.status(200), ctx.json({}), ctx.delay(300));
}) It works absolutely fine with https://www.diffchecker.com/eG2mP74P/ Appreciate everything about MSW 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
From looking at the diff, I'm guessing it's related to the // v0.36.8
case 'MOCK_SUCCESS': {
return delayPromise(
() => respondWithMock(clientMessage),
clientMessage.payload.delay,
)
}
function delayPromise(cb, duration) {
return new Promise((resolve) => {
setTimeout(() => resolve(cb()), duration)
})
}
// v1.3.5
case 'MOCK_RESPONSE': {
return respondWithMock(clientMessage.data)
}
async function respondWithMock(response) {
await sleep(response.delay)
return new Response(response.body, response)
} |
Beta Was this translation helpful? Give feedback.
-
Hi, @cmacdonnacha 👋 Great to see you updating. First thing to point out, v0 and v1 are practically the same, in a sense that v1 doesn't include any breaking changes (the package got bumped to v1 accidentally and we rolled with it because the APIs have been stabled for years at that point). MSW never had any implicit delays, you always have to define one yourself. In your case, I recommend adding a delay to the matching request handler only in the disabled button state test case. Rely on the fallthrough so you don't have to repeat the resolver implementation: test('...', async () => {
server.use(
rest.get('/resource', async (req, res, ctx) => {
await ctx.delay('infinite')
})
)
}) Since that handler doesn't return any mocked responses, MSW will run it, wait for the delay, and normally continue with running the matching base handler. But here, I recommend using the infinite delay mode not to subject your tests to flakiness related to the exact delay duration. |
Beta Was this translation helpful? Give feedback.
Hi, @cmacdonnacha 👋 Great to see you updating.
First thing to point out, v0 and v1 are practically the same, in a sense that v1 doesn't include any breaking changes (the package got bumped to v1 accidentally and we rolled with it because the APIs have been stabled for years at that point).
MSW never had any implicit delays, you always have to define one yourself.
In your case, I recommend adding a delay to the matching request handler only in the disabled button state test case. Rely on the fallthrough so you don't have to repeat the resolver implementation: