Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waitFor is not working with jest fakeTimers #1208

Open
kushuh opened this issue May 8, 2023 · 1 comment
Open

waitFor is not working with jest fakeTimers #1208

kushuh opened this issue May 8, 2023 · 1 comment

Comments

@kushuh
Copy link

kushuh commented May 8, 2023

  • @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.

@jtomaszewski
Copy link

I guess it's the same as #1187 . Jest since v27 uses new implementation of fake timers, and as described in #1187, testing library doesn't work with them as of the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants