You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, when using fake timers in jest, adding a long delay will result in the response not being resolved. Very short delays, seem to work, but I think this is more by luck.
The repo includes three tests, no delay, short delay and a long delay... the long delay fails. I believe this is down to this line
using a real timer for the setTimeout. This causes jest's fake timers and the real timer to get out of sync.
Changing that line to use the faked global timeout (just for the delay!) allows us to advance any timers and have the promise resolve as expected.
if (response.delay) {
await new Promise((resolve) => {
globalThis.setTimeout(resolve, response.delay)
})
}
Obviously I don't have deep knoledge of this library (thats for BTW!) so am a bit uncertain of the concequenses of making this change. It may mean users of fake timers need to advance the timers when using a delay - but I would sort of expect this behaviour as it's consistent with other timers.
Happy to create a PR if needed.
Expected behavior
All tests to pass regardless of the delay duration.
The text was updated successfully, but these errors were encountered:
Prerequisites
Environment check
msw
versionNode.js version
19.1.0
Reproduction repository
https://github.com/leepowelldev/msw-delay-bug
Reproduction steps
npm test
Current behavior
Currently, when using fake timers in jest, adding a long delay will result in the response not being resolved. Very short delays, seem to work, but I think this is more by luck.
The repo includes three tests, no delay, short delay and a long delay... the long delay fails. I believe this is down to this line
msw/src/node/SetupServerApi.ts
Line 91 in 7380011
setTimeout
. This causes jest's fake timers and the real timer to get out of sync.Changing that line to use the faked global timeout (just for the delay!) allows us to advance any timers and have the promise resolve as expected.
Obviously I don't have deep knoledge of this library (thats for BTW!) so am a bit uncertain of the concequenses of making this change. It may mean users of fake timers need to advance the timers when using a delay - but I would sort of expect this behaviour as it's consistent with other timers.
Happy to create a PR if needed.
Expected behavior
All tests to pass regardless of the delay duration.
The text was updated successfully, but these errors were encountered: