Skip to content

Calling sinon.useFakeTimers() inside a test causes Vitest to hang #7790

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

Open
6 tasks done
JoshuaKGoldberg opened this issue Apr 3, 2025 · 4 comments
Open
6 tasks done

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Apr 3, 2025

Describe the bug

Calling sinon.useFakeTimers() in a test causes the test to hang:

> test
> vitest


 DEV  v3.1.1 /Users/josh/repos/repros


 ❯ index.test.js 0/1

 Test Files 0 passed (1)
      Tests 0 passed (1)
   Start at 09:26:11
   Duration 12.34s

After a minute it exits with an error:


 DEV  v3.1.1 /Users/josh/repos/repros

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: [vitest-worker]: Timeout calling "snapshotSaved"
 ❯ Object.onTimeoutError node_modules/vitest/dist/chunks/rpc.DGgL5dw7.js:62:10
 ❯ Timeout._onTimeout node_modules/vitest/dist/chunks/index.68735LiX.js:55:41
 ❯ listOnTimeout node:internal/timers:594:17
 ❯ processTimers node:internal/timers:529:7

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯


 Test Files   (1)
      Tests   (1)
     Errors  1 error
   Start at  09:27:40
   Duration  60.19s (transform 11ms, setup 0ms, collect 17ms, tests 0ms, environment 0ms, prepare 36ms)

Reproduction

Minimum reproduction: https://github.com/JoshuaKGoldberg/repros/tree/vitest-hanging-with-sinon-useFakeTimers

The full use case is a package that wraps a Sinon API: JoshuaKGoldberg/sinon-timers-repeatable#894

System Info

System:
    OS: macOS 13.2
    CPU: (10) arm64 Apple M1 Max
    Memory: 132.53 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 22.12.0 - ~/.nvm/versions/node/v22.12.0/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 10.9.0 - ~/.nvm/versions/node/v22.12.0/bin/npm
    pnpm: 10.6.1 - ~/.nvm/versions/node/v22.12.0/bin/pnpm
  Browsers:
    Chrome: 134.0.6998.166
    Safari: 16.3
  npmPackages:
    vitest: ^3.1.1 => 3.1.1

Used Package Manager

npm

Validations

@AriPerkkio
Copy link
Member

Does it mock nextTick too? node:child_process relies on it and hangs when mocked.

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Apr 4, 2025

Sinon fake timer's default is now easier to break Node either due to nextTick or queueMicrotask cf. #7288, sinonjs/fake-timers#507, so this is likely expected and out of scope for Vitest.

For our own Vitest's internal sinon fake timer usage for vi.useFakeTimers, we've explicitly removed nextTick and queueMicrotask by default to avoid such breakage.

@sheremet-va
Copy link
Member

We can maybe throw an error if nextTick/queueMicrotask is not the same as it was at the start of the program when we communicate via rpc 🤔

@AriPerkkio
Copy link
Member

I'm not sure we are able to send the error from child process to main one. I think I tried restoring nextTick back to original one, but some internals of Node were still hanging there. But sure we can try that here.

Earlier discussion from 2023 with minimal repro without Vitest, just node:child_process + @sinonjs/[email protected]:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants