Components: Refactor more tests to use real timers#47318
Conversation
|
Size Change: 0 B Total Size: 1.33 MB ℹ️ View Unchanged
|
|
Flaky tests detected in ce24334. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3968077875
|
| const menu = screen.getByRole( 'menu', { | ||
| name: 'Block Name', | ||
| } ); | ||
| expect( menu ).toBeInTheDocument(); | ||
| expect( menu ).not.toBeVisible(); | ||
| await waitFor( () => | ||
| expect( | ||
| screen.getByRole( 'menu', { | ||
| name: 'Block Name', | ||
| } ) | ||
| ).toBeVisible() | ||
| ); |
There was a problem hiding this comment.
I'm not sure I understand these changes — previously the test was checking for the menu to be in the document, but not visible, while the new version of the test seems to check that the menu is visible.
Can you explain the rationale here? Thank you!
There was a problem hiding this comment.
The previous test was just wrong. If you manually go through the steps, you'll see that both the button and the menu should be visible. The previous test just wasn't properly waiting for the menu to be revealed.
| const menu = screen.getByRole( 'menu', { | ||
| name: 'Block Name', | ||
| } ); | ||
| expect( menu ).toBeInTheDocument(); | ||
| expect( menu ).not.toBeVisible(); | ||
| await waitFor( () => | ||
| expect( | ||
| screen.getByRole( 'menu', { | ||
| name: 'Block Name', | ||
| } ) | ||
| ).toBeVisible() | ||
| ); |
What?
This PR updates even more component tests to use real timers. This is a follow-up to #46714, #47056, #47144, and #47218.
Why?
We discovered that Jest real timers are preferable when their use is possible, see facebook/react#25889 and the description in #46714.
This post is great writing that explains the rationale in more detail:
https://jsnajdr.wordpress.com/2023/01/11/prefer-jest-real-timers-when-testing-with-react-testing-library/
How?
We're removing the specific
jest.useFakeTimers();calls. We're also removing the now unnecessaryjest.advanceTimersByTime()calls and post-test cleanup that we've been doing withjest.runOnlyPendingTimers()andjest.useRealTimers(). We're also altering some tests to wait for the appropriate actions before continuing/performing the relevant assertions.Testing Instructions
Verify all tests still pass.
Testing Instructions for Keyboard
None
Screenshots or screencast
None.