Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fake timers with long delay fail #1505

Open
4 tasks done
leepowelldev opened this issue Dec 23, 2022 · 1 comment
Open
4 tasks done

Fake timers with long delay fail #1505

leepowelldev opened this issue Dec 23, 2022 · 1 comment
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@leepowelldev
Copy link

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 14 or higher

Node.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

setTimeout(resolve, response.delay)
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.

@leepowelldev leepowelldev added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Dec 23, 2022
@leepowellnbs
Copy link

Update - I did figure out a workaround is to use real timers to wait for the delay to complete... not ideal, but seems to work around the issue.

function wait(ms: number) {
  return new Promise((resolve) => {
    jest.requireActual('timers').setTimeout(resolve, ms);
  });
}

Then in my test, wait for the delay duration to complete with real timers.

await act(() => wait(1000));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
Development

No branches or pull requests

2 participants