Skip to content

Inconsistent behavior with waitFor when using fake timers globally vs per-test #1784

Open
@lingokids-carlos-garcia

Description

@lingokids-carlos-garcia

Describe the bug

waitFor and functions that use it behind-the-scenes like findBy can produce inconsistent results when fake timers are enabled globally versus per-test. This specifically affects tests that render components with async mount effects that trigger state updates. When fake timers are enabled globally in jest.config.js, the test behavior may differ compared to enabling them within individual test files using jest.useFakeTimers().

Example:

const MyComponent = () => {
  const [count, setCount] = React.useState(0);

  const myPromise = async () => {
    const response = await new Promise<number>((resolve) => resolve(1));
    setCount(response);
  };

  React.useEffect(() => {
    myPromise();
  }, []);

  return count > 0 && <Text testID="count">{count}</Text>;
};

Expected behavior

Tests produce the same result independently to enable fake timers globally.

Steps to Reproduce

A couple of branches have been created to demonstrate this case:

Screenshots

Case: Test fails when enabling fake timers globally

Image

Case: Test passes when enabling fake timers on the test

Image

Versions

  npmPackages:
    react: 19.0.0 => 19.0.0
    react-native: 0.79.1 => 0.79.1
    react-test-renderer: 19.0.0 => 19.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions