Skip to main content
The act function wraps test code that renders or updates components, ensuring all effects and renders are flushed before continuing.

Signature

() => void | Promise<void>
required
The test code to run. Can be synchronous or return a Promise.
Returns a Promise that resolves when all effects and renders have been flushed.

How it works

From test-utils/src/index.js:27-112:
  1. Calls setupRerender() to enable synchronous rendering
  2. Overrides options.requestAnimationFrame to capture effect callbacks
  3. Executes your callback
  4. Flushes all pending renders
  5. Flushes all pending effects
  6. Repeats until no more work is pending
  7. Calls teardown() to restore options
The function supports nested calls and both sync and async callbacks.

Usage

Basic usage

Testing state updates

Testing effects

Async callbacks

Implementation details

From test-utils/src/index.js:27-112:
The function uses a depth counter to handle nested act() calls. Only the outermost call performs flushing.

Best practices

Always await act

Wrap all renders and updates

Use with testing libraries