replacing `fireEvent.click` with `userEvent.click` fails test
See original GitHub issue@testing-library/user-event
version: 13.1.9
- Testing Framework and version: jest 27.0.6
- DOM Environment: jsdom
Relevant code or config
We wrote a minimal unit test reproducing the issue.
Commenting out line 46 or 47 makes the test either pass or fail, the only difference being userEvent.click
or fireEvent.click
. It works with fireEvent
but not userEvent
, which seems odd.
To run the test, see instructions below under the reproduction repository.
What you did:
replace fireEvent.click
with userEvent.click
What happened:
test goes from passed to failed
Reproduction repository: https://github.com/boilund/practical-react-components.git
git clone https://github.com/boilund/practical-react-components.git
cd practical-react-components
git checkout nm/unit-test
yarn
yarn setup
yarn workspace practical-react-components-core jest packages/core/src/Select/Select_unit.test.tsx
Problem description:
When using fireEvent.click
in our test, the menu item gets clicked and the test passes. When we try to replace this with userEvent.click
, the test fails. We were not successful in finding the cause of the problem. It seems that the userEvent.click
function eventually calls fireEvent.click
, but we’re at a loss to explain why nothing happens after that. We need help analyzing the cause of the problem.
Suggested solution: Help us diagnose the issue, maybe point out something we missed, or let us know what we can do more to help investigate.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:8 (3 by maintainers)
Top GitHub Comments
It is because now the userEvent is async. this will work
await userEvent.click(/* some code */)
Hello guys,
I had similar issues when I wanted to use userEvents instead of fireEvents.
here is the versions I’m using.
“@testing-library/react”: “^13.1.1”, “@testing-library/user-event”: “^14.1.1”,
User events seems use asynchronous functions ( This seems logical since we want to reproduce user interactions ), so you need use async and await when you invoke functions from this lib in your test. This is what I did for resolve my issue.