CodePark/ questions / Testing
START PRACTICINGPRACTICE
Frontend question · React › Testing

What does act() do in React tests, and why do you rarely call it directly with React Testing Library?

act() makes sure React has finished all updates before you check the DOM. With RTL (React Testing Library), you rarely call it, because RTL already does it for you.

act(callback) processes every update the callback causes, such as renders, state updates and effects. It applies them to the DOM before your assertions run. Without it, your test could check the DOM before React finished. React also warns about updates not wrapped in act.

In React 19, import act from react. The version from react-dom/test-utils is deprecated. Use await act(async () => ...) when the work is async.

RTL already wraps render, fireEvent and its user-event integration in act. For updates that happen later, like data arriving after a fetch, use findBy queries or waitFor. They retry until the assertion passes.

An act warning in an RTL test usually means an update happened after the test stopped waiting.

  • act processes renders, effects and updates before your assertions.
  • In React 19, import it from react, and use the async form with await.
  • RTL already wraps render and events in act.
  • Use findBy or waitFor for async updates.
  • An act warning usually means an update you did not wait for.