fix: add Vitest fake timer detection to asyncWrapper#1443
Open
penkzhou wants to merge 1 commit intotesting-library:mainfrom
Open
fix: add Vitest fake timer detection to asyncWrapper#1443penkzhou wants to merge 1 commit intotesting-library:mainfrom
penkzhou wants to merge 1 commit intotesting-library:mainfrom
Conversation
The asyncWrapper's setTimeout(0) drain mechanism requires advancing fake timers when they are active. Previously, only Jest fake timers were detected (via `jest` global), causing Vitest users with fake timers to experience hangs in `waitFor` and `user.click` (see testing-library#1197, testing-library#1187). This adds a `vitestFakeTimersAreEnabled()` function that checks for the `vi` global (Vitest's equivalent of `jest`) combined with the `setTimeout.clock` property set by @sinonjs/fake-timers. When Vitest fake timers are detected, `vi.advanceTimersByTime(0)` is called to unblock the drain promise. The detection is extracted into a unified `advanceFakeTimers()` helper that handles both Jest and Vitest paths, keeping the asyncWrapper itself clean. Closes testing-library#1197 Relates to testing-library#1187
|
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. Latest deployment of this branch, based on commit 8a926ae:
|
Author
|
I encountered this leak during development. Since the source of the leak is within this specific library, I would like to have it fixed so that more people can benefit from the solution. cc @eps1lon @MichaelDeBoey |
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.
What: Add Vitest fake timer detection to the
asyncWrapperinpure.js, so thatwaitFor(and by extensionuser.clickvia@testing-library/user-event) no longer hangs when Vitest fake timers are active.Why: The
asyncWrapperusessetTimeout(0)to drain the microtask queue after a callback completes. When fake timers are active, thissetTimeoutnever fires unless the test framework advances timers. The existing code only checks for Jest fake timers (via thejestglobal), so Vitest users withvi.useFakeTimers()experience indefinite hangs inwaitForanduser.click. This is the root cause of #1197 and relates to #1187.How:
vitestFakeTimersAreEnabled()— checks for theviglobal ANDsetTimeout.clock(set by@sinonjs/fake-timers, which Vitest uses internally). Both conditions must be true to avoid false positives when Vitest is present but real timers are in use.advanceFakeTimers()— a unified helper that tries Jest first, then Vitest, keeping theasyncWrapperitself clean.asyncWrapper'ssetTimeout(0)+ advance pattern is unchanged; only the detection scope is widened.setTimeout.clockand_isMockFunctionpresence).Note: The Vitest code path cannot be fully integration-tested within Jest (since
jestis always available as a global and takes priority in detection). The detection logic is verified via unit tests that assert the signals each framework sets onsetTimeout. Full Vitest integration was verified manually in a separate Vitest project.Checklist:
Closes #1197
Relates to #1187