-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Components: Use real timers where possible #47056
Conversation
Size Change: +45 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
Flaky tests detected in 8e64eb4. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3901901345
|
I think we only prefer real times because of the bug @jsnajdr discovered. How does this affect JS Unit test runtime? But the difference probably won't be too big. |
jest.advanceTimersByTime( ms + 1 ); | ||
return promise; | ||
}; | ||
const sleep = ( ms ) => new Promise( ( resolve ) => setTimeout( resolve, ms ) ); |
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.
This should probably get replaced with something like:
await waitFor( () => expect( onChange ).toHaveBeenCalled() );
at all the places where sleep()
is used.
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.
Thanks for the suggestion, done in 8e64eb4
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.
There are still the "we need to sleep for at least 1ms" comments that no longer make any sense.
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.
I mean, they make sense IMHO but I'm happy to remove them.
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.
Removed in 5b2ae5a
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.
You're right, now I realize the actually do make sense. I apologize for being so blunt.
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.
No worries. I think you have a point that the waitFor()
calls should be self-explanatory, making the comment redundant.
There are also other, semi-philosophical but IMO very good reasons to prefer real timers when working with React Testing Library. Here I'm posting publicly what was originally an internal P2 comment in a discussion about Tumblr, but it applies universally: https://jsnajdr.wordpress.com/2023/01/11/prefer-jest-real-timers-when-testing-with-react-testing-library/ |
Thanks for sharing, @jsnajdr 🙇 |
Thank you all for working on this, and special thanks to @jsnajdr for sharing his detailed post re. fake vs real timers 👏 |
What?
This PR updates component tests to use real timers where possible. This is a follow-up to #46714.
Why?
We discovered that Jest real timers are preferable when their use is possible, see facebook/react#25889 and the description in #46714.
How?
We're removing the specific
jest.useFakeTimers();
calls, and the unnecessaryadvanceTimers: jest.advanceTimersByTime
in the user-event setup. We're also removing the now unnecessaryjest.advanceTimersByTime()
calls and post-test cleanup that we've been doing withjest.runOnlyPendingTimers()
andjest.useRealTimers()
.Testing Instructions
Verify all tests still pass.
Testing Instructions for Keyboard
None
Screenshots or screencast
None.