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.
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.
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.
I mean, they make sense IMHO but I'm happy to remove them.
There was a problem hiding this comment.
You're right, now I realize the actually do make sense. I apologize for being so blunt.
There was a problem hiding this comment.
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.advanceTimersByTimein 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.