fireEvent.click runs but nothing changes in the test
Unable to find an element with the text: ClickedShort 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.
Fix it fast
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()2Match what you see, then jump to its fix
- 1
The text appears if you add a delay, or the assertion passes when you await itThe assertion runs before an async update finishesFix - 2
The component uses onMouseDown, onPointerDown, onFocus or a disabled check, and fireEvent.click does not trigger itThe handler needs more than a click eventFix - 3
screen.debug() shows the click target is a wrapper, hidden node or another buttonThe query returned a different element than you meantFix
- 1
What does the error look like?
Unable to find an element with the text: Clickedexpect(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.jsx2Fix
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.jsxConfirmed by testing-library.com
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.jsxConfirmed by testing-library.com
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.jsxConfirmed by testing-library.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
A React test warns that a state update was not wrapped in act()
Warning: The current testing environment is not configured to support act(...)
The warning means React state changed while your test was not waiting for it.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix