feat: automatically advance fake timers in Vitest#1304
feat: automatically advance fake timers in Vitest#1304TrevorBurnham wants to merge 1 commit intotesting-library:mainfrom
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
|
Before this is merged you'll probably want to fix the circular import of When using this is the intent to have jest in the languageOptions.globals? Or for earlier versions of jest in a set up file: |
66cbd51 to
2e77fcc
Compare
|
@JoueBien Thanks! I've updated the PR branch. |
|
Just wondering if the plan is still to land this fix. We're seeing this in a large monorepo in which we'd like to incrementally migrate from Jest to Vitest. |
2e77fcc to
d4c8ab5
Compare
|
I've rebased the branch. The build failure in CI appears to be unrelated to this change: |
|
Looking at that error I'd probably try the following things:
|
This PR adds automatic detection of fake timers from both Jest and Vitest, eliminating the need for users to manually configure
advanceTimerswhen usingvi.useFakeTimers()orjest.useFakeTimers().Fixes #1115
Problem
When using Vitest with fake timers,
userEventwould timeout because it wasn't advancing the fake timers. Users had to work around this by shimming the Jest global:This happened because
userEventonly checked for Jest's timer APIs, not Vitest's.Solution
Auto-detect Jest and Vitest timer globals so
advanceTimersdoesn't need to be configured manually.getTimerAdvancer()insrc/utils/misc/timerDetection.ts— checksglobalThis.jestandglobalThis.vifor anadvanceTimersByTimemethod and returns a bound reference to it (ornull).wait.tscallsgetTimerAdvancer()when the user hasn't provided a customadvanceTimersoption, falling back to the default no-op if no framework is detected.advanceTimersconfiguration still takes precedence over auto-detection.Before
After