Maaz Zindani

Loading the index…

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

A React test warns that a state update was not wrapped in act()

ErrorWarning: An update to <Component> inside a test was not wrapped in act(...)

Short answer

The warning means React state changed while your test was not waiting for it. Most often an async update lands after the assertion ran, so wait with findBy or waitFor. If you use react-dom directly, wrap renders and events in await act(async ...). Missing test setup and leftover mounted components can also cause it.

  • Applies to React 19, @testing-library/react 16, Jest 29, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find which component is named in the warning, then look at the line in your test just before the warning appears. Ask one question: is there an await between the action and the assertion?

    npx jest path/to/Component.test.jsx --runInBand
  2. 2Match what you see, then jump to its fix

    1. 1The warning names a component that sets state after a fetch, timer or promise, and the test asserts right after the click or render.The assertion runs before an async update has finishedFix
    2. 2The test uses createRoot(...).render or dispatchEvent itself instead of React Testing Library.You render or fire events with react-dom directly, outside actFix
    3. 3The warning text says the current testing environment is not configured to support act(...).The test environment is not marked as an act environmentFix
    4. 4The warning appears in a test that does not touch the named component, or only when the whole file runs.A component from a previous test is still mountedFix

What does the error look like?

  • Warning: An update to <Component> inside a test was not wrapped in act(...)
  • Warning: The current testing environment is not configured to support act(...)

Cause 1 of 4

The assertion runs before an async update has finished

1Check

Look for a synchronous getBy... or expect(...) straight after an action that triggers async work.

grep -n "getBy\|expect(" path/to/Component.test.jsx

2Fix

Make the test wait for the visible result. findBy queries combine a query with waiting, and waitFor retries a callback that throws until it passes or times out.

// before
fireEvent.click(screen.getByRole('button', { name: 'Load' }));
expect(screen.getByText('Done')).toBeInTheDocument();

// after
fireEvent.click(screen.getByRole('button', { name: 'Load' }));
expect(await screen.findByText('Done')).toBeInTheDocument();

3Verify

Run the test again. The warning should be gone and the test should still pass.

npx jest path/to/Component.test.jsx

Confirmed by testing-library.com

Did this fix it?

Cause 2 of 4

You render or fire events with react-dom directly, outside act

1Check

Search the test for createRoot, dispatchEvent or a hand-made container.

grep -n "createRoot\|dispatchEvent" path/to/Component.test.jsx

2Fix

Put the render and each event inside await act(async () => { ... }). React documents the async form as the recommended one; the sync form is going away. Or switch to the render and fireEvent helpers from React Testing Library.

import { act } from 'react';
import { createRoot } from 'react-dom/client';

await act(async () => {
  createRoot(container).render(<Counter />);
});
await act(async () => {
  button.dispatchEvent(new MouseEvent('click', { bubbles: true }));
});

3Verify

Assert after the act calls. The text on screen should show the updated state.

npx jest path/to/Component.test.jsx

Confirmed by react.dev

Did this fix it?

Cause 3 of 4

The test environment is not marked as an act environment

1Check

You get this when you use act without React Testing Library, because the library sets the flag for you.

grep -rn "IS_REACT_ACT_ENVIRONMENT" jest.setup.js src/ 2>/dev/null

2Fix

Set the global flag once in a setup file that runs before your React tests.

// setup file that runs before your React tests
global.IS_REACT_ACT_ENVIRONMENT = true;

3Verify

Run the suite. The configuration warning should not appear again.

npx jest

Confirmed by react.dev

Did this fix it?

Cause 4 of 4

A component from a previous test is still mounted

1Check

React Testing Library unmounts after each test only when your framework exposes a global afterEach. If it does not, you must call cleanup yourself.

npx jest --listTests | head

2Fix

Make sure your runner exposes global hooks (Jest does), or call cleanup in your own afterEach.

import { cleanup } from '@testing-library/react';
afterEach(cleanup);

3Verify

Run the whole file, not only one test. No update warning should appear.

npx jest path/to/Component.test.jsx

Confirmed by testing-library.com

Did this fix it?

How do I stop it coming back?

  • End each async test on a findBy query or a waitFor assertion, so the test only finishes when the screen reached its final state.
  • Import render, screen and fireEvent from React Testing Library instead of driving react-dom by hand.
  • Keep one setup file for global test flags so every test file gets the same environment.

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.