react-testing-library
react-testing-library copied to clipboard
waitFor is not working with jest fakeTimers
-
@testing-library/reactversion: v14.0.0 - Testing Framework and version:
-
jestversion 29.5.0
-
- DOM Environment:
-
jsdomversion 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.

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

Reproduction:
I made a basic repro here.
Problem description:
Using jest fake timers breaks the waitFor function.
I guess it's the same as https://github.com/testing-library/react-testing-library/issues/1187 . Jest since v27 uses new implementation of fake timers, and as described in https://github.com/testing-library/react-testing-library/issues/1187, testing library doesn't work with them as of the moment.