Skip to main content
The setupRerender function configures Preact to render synchronously, making it easier to test components without dealing with asynchronous behavior.

Signature

Returns a function that manually triggers pending renders.

How it works

From test-utils/src/index.js:7-11:
The function:
  1. Saves the original options.debounceRendering function
  2. Replaces it with a version that captures the render callback
  3. Returns a function that executes the captured callback
This allows you to control exactly when renders happen in your tests.

Usage

Basic setup

With state updates

Test suite setup

When to use

Use setupRerender when:

  • You want fine-grained control over rendering
  • You’re not using act()
  • You’re writing low-level test utilities
  • You need to test render timing

Use act instead when:

  • You want automatic effect flushing
  • You’re testing user interactions
  • You’re testing async code
  • You want compatibility with React testing patterns
Most tests should use act() instead of setupRerender() directly. The act() function calls setupRerender() internally and also handles effects.

teardown function

Always call teardown() after using setupRerender() to restore Preact’s default behavior:
From test-utils/src/index.js:117-130:
The teardown function:
  1. Flushes any pending renders
  2. Restores the original debounceRendering function
  3. Cleans up test state
Forgetting to call teardown() can cause test interference where one test’s render behavior affects another.

Example: Testing framework integration

Best practices

Always clean up

Flush after renders

Use act for effects