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.
How it works
From test-utils/src/index.js:27-112:- Calls
setupRerender()to enable synchronous rendering - Overrides
options.requestAnimationFrameto capture effect callbacks - Executes your callback
- Flushes all pending renders
- Flushes all pending effects
- Repeats until no more work is pending
- Calls
teardown()to restore options
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.