Test initial rendering
A test renders a component and needs to assert its initial DOM output after React has committed the render.
React 19+ · Function
act is a test helper that flushes React updates caused by rendering and interactions before assertions. Use its asynchronous form to make test behavior closer to React in the browser.
Use the async act helper around component rendering and interactions in tests. React queues updates triggered inside the callback, flushes them—including updates across async boundaries—and applies the resulting changes before assertions.
await act(async actFn)| Name | Type | Description |
|---|---|---|
async actFn | function | An async function wrapping renders or interactions for components being tested. Updates it triggers are queued and flushed together; asynchronous boundaries are also processed. |
Nothing.
import { act } from 'react';
import { createRoot } from 'react-dom/client';
const container = document.createElement('div');
const root = createRoot(container);
function Counter() {
return <button onClick={() => root.render(<p>Updated</p>)}>Update</button>;
}
await act(async () => {
root.render(<Counter />);
container.querySelector('button').click();
});
console.assert(container.textContent === 'Updated');Wrap rendering and interactions in async act so React processes their updates before the assertion.
A test renders a component and needs to assert its initial DOM output after React has committed the render.
A test dispatches a click or other interaction and checks the resulting UI.
A component schedules work across an asynchronous boundary that must settle before assertions.
act is a test helper that flushes React updates caused by a unit of interaction before assertions. Wrapping rendering or an event in await act(async () => { ... }) makes the test closer to browser behavior.
Follow-up: Why should the callback be asynchronous? It lets React process code that crosses an async boundary and flush scheduled updates before the test continues.
The callback receives no required arguments and should contain the render or interaction being tested. Updates triggered inside it enter an internal queue that React flushes together.
Follow-up: What kinds of work belong inside the callback? Rendering, user-event handling, and other updates whose resulting DOM state the test will assert.
act returns nothing. Its purpose is synchronization: after the awaited call completes, the updates caused by the callback have been processed and applied to the DOM.
Follow-up: What environment setting may be required? Set global.IS_REACT_ACT_ENVIRONMENT=true so act is enabled for the test environment.
react-dom/test-utils.act@testing-library/reactReactDOMTestUtils.act