A React test warns that a state update was not wrapped in act()
Warning: 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.
Fix it fast
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 --runInBand2Match what you see, then jump to its fix
- 1
The 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
The test uses createRoot(...).render or dispatchEvent itself instead of React Testing Library.You render or fire events with react-dom directly, outside actFix - 3
The warning text says the current testing environment is not configured to support act(...).The test environment is not marked as an act environmentFix - 4
The 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
- 1
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.jsx2Fix
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.jsxConfirmed by testing-library.com
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.jsx2Fix
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.jsxConfirmed by react.dev
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/null2Fix
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 jestConfirmed by react.dev
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 | head2Fix
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.jsxConfirmed by testing-library.com
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:
- 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.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web 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
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix