-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: add Vitest fake timer detection to asyncWrapper #1443
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
penkzhou
wants to merge
1
commit into
testing-library:main
Choose a base branch
from
penkzhou:fix/async-wrapper-timer-leak-and-vitest-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import * as React from 'react' | ||
| import {render, waitFor, screen} from '../' | ||
|
|
||
| // Regression tests: verify asyncWrapper works correctly with Jest fake timers. | ||
| // These test the advanceFakeTimers() unified path that handles both Jest and Vitest. | ||
| describe.each([ | ||
| ['fake legacy timers', () => jest.useFakeTimers('legacy')], | ||
| ['fake modern timers', () => jest.useFakeTimers('modern')], | ||
| ])('asyncWrapper advances fake timers with Jest %s', (label, useTimers) => { | ||
| beforeEach(() => { | ||
| useTimers() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers() | ||
| }) | ||
|
|
||
| const fetchData = () => | ||
| new Promise(resolve => { | ||
| setTimeout(() => resolve('Hello World'), 100) | ||
| }) | ||
|
|
||
| function AsyncComponent() { | ||
| const [data, setData] = React.useState(null) | ||
| React.useEffect(() => { | ||
| let cancelled = false | ||
| fetchData().then(result => { | ||
| if (!cancelled) { | ||
| setData(result) | ||
| } | ||
| }) | ||
| return () => { | ||
| cancelled = true | ||
| } | ||
| }, []) | ||
| if (!data) return <div>Loading</div> | ||
| return <div data-testid="result">{data}</div> | ||
| } | ||
|
|
||
| test('waitFor resolves when data loads asynchronously', async () => { | ||
| render(<AsyncComponent />) | ||
| expect(screen.getByText('Loading')).toBeInTheDocument() | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId('result')).toHaveTextContent('Hello World') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| // Verify asyncWrapper works with real timers (no fake timer detection needed) | ||
| describe('asyncWrapper with real timers', () => { | ||
| beforeEach(() => { | ||
| jest.useRealTimers() | ||
| }) | ||
|
|
||
| function MicrotaskComponent() { | ||
| const [show, setShow] = React.useState(false) | ||
| React.useEffect(() => { | ||
| Promise.resolve().then(() => setShow(true)) | ||
| }, []) | ||
| if (show) { | ||
| return <div data-testid="result">Done</div> | ||
| } | ||
| return <div>Loading</div> | ||
| } | ||
|
|
||
| test('waitFor resolves with microtask-based updates', async () => { | ||
| render(<MicrotaskComponent />) | ||
| await waitFor(() => { | ||
| expect(screen.getByTestId('result')).toHaveTextContent('Done') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| // Unit tests for the fake timer detection helpers. | ||
| // These test the detection logic directly since we can't fully simulate | ||
| // a Vitest environment from within Jest (jest global is always injected | ||
| // into module scope by the test runner). | ||
| describe('fake timer detection logic', () => { | ||
| test('jestFakeTimersAreEnabled returns true with modern fake timers', () => { | ||
| jest.useFakeTimers('modern') | ||
| // setTimeout.clock is set by @sinonjs/fake-timers (used by Jest modern timers) | ||
| expect( | ||
| // eslint-disable-next-line prefer-object-has-own | ||
| Object.prototype.hasOwnProperty.call(setTimeout, 'clock'), | ||
| ).toBe(true) | ||
| jest.useRealTimers() | ||
| }) | ||
|
|
||
| test('jestFakeTimersAreEnabled returns true with legacy fake timers', () => { | ||
| jest.useFakeTimers('legacy') | ||
| // Legacy timers use @sinonjs/fake-timers which attaches a clock property | ||
| expect( | ||
| // eslint-disable-next-line prefer-object-has-own | ||
| Object.prototype.hasOwnProperty.call(setTimeout, 'clock'), | ||
| ).toBe(true) | ||
| jest.useRealTimers() | ||
| }) | ||
|
|
||
| test('setTimeout.clock is absent with real timers', () => { | ||
| jest.useRealTimers() | ||
| expect( | ||
| // eslint-disable-next-line prefer-object-has-own | ||
| Object.prototype.hasOwnProperty.call(setTimeout, 'clock'), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| test('vi global is not defined by default in Jest', () => { | ||
| // This verifies that in Jest, the Vitest detection path is not triggered | ||
| // because `vi` is not defined. In a real Vitest environment, `vi` would | ||
| // be available and vitestFakeTimersAreEnabled() would check for | ||
| // setTimeout.clock to determine if fake timers are active. | ||
| expect(typeof vi).toBe('undefined') | ||
| }) | ||
| }) |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
vinon-existent by default? https://v1.vitest.dev/config/#globals