waitFor is not working with jest fakeTimers
kushuh opened this issue · comments
@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.
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 #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.