act is a test helper to apply pending React updates before making assertions.
await act(async actFn)To prepare a component for assertions, wrap the code rendering it and performing updates inside an await act() call. This makes your test run closer to how React works in the browser.
Reference
await act(async actFn)
When writing UI tests, tasks like rendering, user events, or data fetching can be considered as “units” of interaction with a user interface. React provides a helper called act() that makes sure all updates related to these “units” have been processed and applied to the DOM before you make any assertions.
The name act comes from the Arrange-Act-Assert pattern.
it ('renders with button disabled', async () => {
await act(async () => {
root.render(<TestComponent />)
});
expect(container.querySelector('button')).toBeDisabled();
});Parameters
async actFn: An async function wrapping renders or interactions for components being tested. Any updates triggered within theactFn, are added to an internal act queue, which are then flushed together to process and apply any changes to the DOM. Since it is async, React will also run any code that crosses an async boundary, and flush any updates scheduled.
Returns
act does not return anything.
Usage
When testing a component, you can use act to make assertions about its output.
For example, let’s say we have this Counter component, the usage examples below show how to test it:
function Counter() {
const [count, setCount] = useState(0);
const handleClick = () => {
setCount(prev => prev + 1);
}
useEffect(() => {
document.title = `You clicked ${count} times`;
}, [count]);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={handleClick}>
Click me
</button>
</div>
)
}Rendering components in tests
To test the render output of a component, wrap the render inside act():
import {act} from 'react';