Skip to content

waitFor is not working with jest fakeTimers #1208

Open
@kushuh

Description

@kushuh
  • @testing-library/react version: v14.0.0
  • Testing Framework and version:
    • jest version 29.5.0
  • DOM Environment:
    • jsdom version 29.5.0

Relevant code or config:

Here is the test code (designed to fail):

import {renderHook, waitFor} from "@testing-library/react";
import {useState} from "react";

beforeAll(() => {
  jest.useFakeTimers();
});

describe("dummy test", () => {
  it("I will fail", async () => {
    const { result } = renderHook(() => {
      const [data] = useState(false);
      return { data };
    });

    await waitFor(
      () => {
        // Will obviously fail, but should do so after 5s.
        expect(result.current.data).toBeTruthy();
      },
      { timeout: 5000 }
    );
  });
});

What you did:

Running tests with npm run test.

What happened:

The test failed nearly instantly.

image

However, if I comment the jest.useFakeTimers() line, it works as expected.

image

Reproduction:

I made a basic repro here.

Problem description:

Using jest fake timers breaks the waitFor function.

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