feat: automatically advance fake timers in Vitest#1304
Open
TrevorBurnham wants to merge 1 commit intotesting-library:mainfrom
Open
feat: automatically advance fake timers in Vitest#1304TrevorBurnham wants to merge 1 commit intotesting-library:mainfrom
TrevorBurnham 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
Author
|
@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
Author
|
I've rebased the branch. The build failure in CI appears to be unrelated to this change: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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