question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
0xHeckercommented, Aug 30, 2022

It is because now the userEvent is async. this will work await userEvent.click(/* some code */)

5reactions
redouredoucommented, Apr 27, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Testing Library with userEvent.click wrong act() warning
So I replaced userEvent.click and went back to fireEvent.click , plus your fix ... I've had a working test fail just by adding...
Read more >
user-event v13 - Testing Library
Pointer events options​. Trying to click an element with pointer-events being set to "none" (i.e. unclickable) will throw an error.
Read more >
React-testing-library: fireEvent vs userEvent - mimacom blog
With an appropriate testing we can adjust and change our code knowing ... We can find inside the userEvent object events such as...
Read more >
replacing `fireEvent.click` with `userEvent.click` fails test
replacing `fireEvent.click` with `userEvent.click` fails test · @testing-library/user-event version: 13.1.9 · Testing Framework and version: jest ...
Read more >
React Testing Library Examples
toBeTruthy(); }); test('calls correct function on click', ... getByLabelText to select elements via aria-label; fireEvent.change to simulate change events.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found