Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

fireEvent.click runs but nothing changes in the test

ErrorUnable to find an element with the text: Clicked

Short answer

fireEvent.click only dispatches one DOM click event. Tests then fail for three reasons: the UI updates asynchronously and you assert too soon, the handler listens to events a click alone does not fire, or the query found a different element. Await findBy or waitFor, switch to userEvent, and query by role.

  • Applies to React Testing Library 16, user-event 14, React 19, Jest 30
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the DOM right after the click. It shows whether the click changed anything and which element you really hit.

    fireEvent.click(screen.getByRole('button', { name: /save/i }))
    screen.debug()
  2. 2Match what you see, then jump to its fix

    1. 1The text appears if you add a delay, or the assertion passes when you await itThe assertion runs before an async update finishesFix
    2. 2The component uses onMouseDown, onPointerDown, onFocus or a disabled check, and fireEvent.click does not trigger itThe handler needs more than a click eventFix
    3. 3screen.debug() shows the click target is a wrapper, hidden node or another buttonThe query returned a different element than you meantFix

What does the error look like?

  • Unable to find an element with the text: Clicked
  • expect(jest.fn()).toHaveBeenCalledTimes(1) Expected number of calls: 1 Received number of calls: 0

Cause 1 of 3

The assertion runs before an async update finishes

1Check

If the handler fetches data or sets state after a promise, the new UI is not there on the next line.

grep -rn "await\|async" src/Save.jsx

2Fix

Use a findBy query, which waits, or wrap the assertion in waitFor. The default timeout is 1000 ms.

fireEvent.click(screen.getByRole('button', { name: /save/i }))
expect(await screen.findByText('Saved')).toBeInTheDocument()

3Verify

Run the single test.

npx jest Save.test.jsx

Confirmed by testing-library.com

Did this fix it?

Cause 2 of 3

The handler needs more than a click event

1Check

fireEvent dispatches DOM events one at a time, while userEvent simulates the full interaction, which can fire several events.

grep -rn "onMouseDown\|onPointerDown\|onFocus" src/

2Fix

Use user-event and call setup() before render. Its methods return promises, so await them.

import userEvent from '@testing-library/user-event'

test('saves', async () => {
  const user = userEvent.setup()
  render(<Save />)
  await user.click(screen.getByRole('button', { name: /save/i }))
})

3Verify

The handler now runs; check the mock was called.

npx jest Save.test.jsx

Confirmed by testing-library.com

Did this fix it?

Cause 3 of 3

The query returned a different element than you meant

1Check

getBy queries throw when nothing or more than one element matches, so a loose matcher may still pick a wrong node.

screen.debug()

2Fix

Query by role and accessible name, which matches what a user clicks. user-event also refuses to click hidden elements or type into disabled boxes, so it exposes this mistake.

await user.click(screen.getByRole('button', { name: 'Save' }))

3Verify

The test fails clearly if the button is missing.

npx jest Save.test.jsx

Confirmed by testing-library.com

Did this fix it?

How do I stop it coming back?

  • Prefer user-event over fireEvent for clicks, typing and keyboard input; the library itself recommends it for most cases.
  • Await every user-event call and every findBy query.
  • Query by role and name instead of class names or test ids where possible.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.