The `act()` Utility: Syncing Tests with React's Updates

The `act()` utility ensures your tests wait for React to finish updating the DOM before you make assertions. Use it to wrap component renders and state updates. The footgun is forgetting to use `await` with an async function, which can lead to flaky tests.
The `act()` utility synchronizes your test runner with React's update cycle, ensuring all state changes and effects have been applied to the DOM before you make assertions. It mimics how React works in a browser. Use it when manually rendering components or dispatching events in tests. The main footgun is not using the async version (`await act(async () => ...)`), which can cause flaky tests because it fails to wait for all asynchronous updates to complete before your assertions run.
Read the original → react.dev
- #react
- #testing
- #jest
- #dom
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.