-
-
Notifications
You must be signed in to change notification settings - Fork 696
test: migrate to Vitest fake timers, remove @testing-library/user-event, and resolve act warnings #3147
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
base: main
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
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. |
commit: |
bae4b43 to
93723d9
Compare
|
| Playground | Link |
|---|---|
| React demo | https://livecodes.io?x=id/BFT2XY2NG |
See documentations for usage instructions.
5d220f5 to
80e3043
Compare
…ByText' with 'getByText', and add fake timers
80e3043 to
9c44611
Compare
0fc2d0e to
c93671a
Compare
…ByText' with 'getByText', and remove 'waitFor'
4c297c0 to
c871edf
Compare
9643b35 to
7f52e9e
Compare
7f52e9e to
4d9a53c
Compare
…t' with 'getByText', add fake timer, and remove 'waitFor'
4d9a53c to
1c953e2
Compare
a6cb1d9 to
278abe2
Compare
|
I feel like I'm talking to AI. My review is not comprehensive, and I need to count on you @sukvvon human to review all tests follow what I suggested. |
…estore microtask delay" This reverts commit 278abe2.
…timer advancement in bail test
…rtions(6)' in setSelf timing test
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.
As I felt like I might overlook something, I reviewed abortable.test.tsx and async.test.tsx again.
tests/react/abortable.test.tsx
Outdated
| render( | ||
| <StrictMode> | ||
| <Suspense fallback="loading"> | ||
| <Suspense fallback={<div>loading</div>}> |
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.
Is this change important? If so, can you create another PR? We would like to reduce diffs so that it reduces the human brain power for the review.
If you use AI coding assistant, asking them to reduce git diff -w output might be helpful.
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.
tests/react/abortable.test.tsx
Outdated
|
|
||
| await userEvent.click(screen.getByText('button')) | ||
| await userEvent.click(screen.getByText('toggle')) | ||
| expect(abortedCount).toBe(0) |
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.
Do we need this line? You can leave it if it's helpful.
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.
tests/react/async.test.tsx
Outdated
| const asyncCountAtom = atom( | ||
| async (get) => get(countAtom), | ||
| async (get) => { | ||
| await new Promise<void>((resolve) => setTimeout(resolve, 100)) |
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.
Actually "it" says "without setTimeout", so maybe we want to remove this and remove "loading" check if necessary.
https://github.com/pmndrs/jotai/blob/b6ada607bd928e54214ebb00c5ef0838f53dfe2e/tests/async.test.tsx is the original commit. We used setTimeout for the following test.
What do you think?
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.
tests/react/async.test.tsx
Outdated
| it('async get and useEffect on parent', async () => { | ||
| const countAtom = atom(0) | ||
| const asyncAtom = atom(async (get) => { | ||
| await new Promise((resolve) => setTimeout(resolve, 100)) |
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.
Maybe this too.
tests/react/async.test.tsx
Outdated
| expect(screen.getByText('loading')).toBeInTheDocument() | ||
| await act(() => vi.advanceTimersByTimeAsync(100)) |
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.
Can remove this if we remove that.
e8b8c0b to
5eb44df
Compare
5eb44df to
4ab610b
Compare
…nc atoms in useEffect tests
4ab610b to
1442388
Compare
Related Bug Reports or Discussions
Fixes #
Summary
This PR migrates all tests from real timers to Vitest fake timers to improve test determinism and execution speed. The migration includes comprehensive updates across 33 test files, eliminates most React act warnings, and removes the
@testing-library/user-eventdependency in favor offireEvent.Key Changes
beforeEach(vi.useFakeTimers)andafterEach(vi.useRealTimers)userEventwithfireEvent- Use synchronous event simulation (userEvent uses real timers internally)setTimeoutdelays - Convert manual promise resolution to timer-controlled delaysfindByText/waitFortogetByTextwith explicit timer controlWhy Replace
userEventwithfireEvent?userEventuses real timers internally, conflicting with fake timersfireEventis synchronous and fully compatible withvi.useFakeTimers()fireEventPattern (Simple Rule)Use
await act(() => fireEvent.click(...))when immediately checking Suspense loading state.Otherwise use plain
fireEvent.click(...).Examples
With Suspense (need act):
Without Suspense (plain fireEvent):
Pro tip: When in doubt, try plain
fireEventfirst. If the test fails or shows act warnings, wrap withawait act().Statistics
package.json@testing-library/user-eventCheck List
pnpm run fixfor formatting and linting code and docs