-
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
Fix async timing in tests using user-event
#40790
Conversation
await
in front of user typing events in Unit Control testsawait
for user interactions during unit tests
await
for user interactions during unit tests
Gotta |
…ove unnecessary `waitFor`
3e9a61d
to
79bdd56
Compare
user-event
Size Change: -14 B (0%) Total Size: 1.23 MB
ℹ️ View Unchanged
|
// TODO: investigate why `onChange` gets called twice instead of once | ||
expect( onChangeSpy ).toHaveBeenCalledTimes( 2 ); |
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 is the only diverging test — although I don't think it should block this PR from being merged, as it's a behavior that can be currently reproduced in trunk
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 agree.
// TODO: investigate why
onChangegets called twice instead of once
I guess it's been that way for quite some time now and somehow the async oversights in the test suite were masking it. We could look at extracting this change(and your added code comment) from #40568 to a separate PR.
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.
Sounds like a plan! Opened #40796 with those changes
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.
Superbly done! It's great to see the update with user-event’s advanceTimers
and the cleanup of the waitFor
s (which I guess had been necessary due to missing await
s in other places).
The only thing I noticed was the typo of “trigger” (x3)
// Clicking document.body to trigget a blur event on the input. |
...RTLrender( jsx ), | ||
}; | ||
} | ||
const user = userEvent.setup( { |
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.
@testing-library
recommends that setup()
is called inline once per test.
https://testing-library.com/docs/user-event/intro#writing-tests-with-userevent
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 must have missed that part when looking at the new v14
docs, thank you for flagging it! Opened #40839 as a follow-up with those changes
What?
Polish/improve unit tests using
@testing-library/user-event
by making sure that weawait
each user interaction properly.Why?
As part of the investigation around fixing a timeout error when using
@testing-library/user-event
(comment), and related to the recent conversations in #40568 (comment) and #40697 (comment)How?
this PR applies a few small tweaks to the unit tests of
UnitControl
andFormFileUpload
. Most notable changes are:@testing-library/user-event
functions are now prefixed withawait
jest.fakeTimers
(as suggested here)waitFor
calls (not needed anymore)Testing Instructions
Review code changes, and make sure that all tests pass.