You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
useFakeTimer returns timeout error when used with waitFor in react 18.
I've been trying to use wairFor and fake timers in a unit test but i get this error message:
I created a repo in github to show this problem and here it is.
Unit test:
import"@testing-library/jest-dom";import{act,render,screen,waitFor}from"@testing-library/react";importExampleComponentfrom"../ExampleComponent";describe("ExampleComponent",()=>{beforeEach(()=>{vi.useFakeTimers();});afterEach(()=>{vi.restoreAllMocks();vi.clearAllTimers();});test("changes message after 1 second",async()=>{render(<ExampleComponent/>);act(()=>vi.advanceTimersByTime(1000));expect(awaitscreen.findByText("Hello, world!")).toBeInTheDocument();});test("changes message after 1 second --- 2",async()=>{render(<ExampleComponent/>);act(()=>vi.advanceTimersByTime(1000));awaitwaitFor(()=>expect(screen.getByText("Hello, world!")).toBeInTheDocument());expect(awaitscreen.getByText("Hello, world!")).toBeInTheDocument();});});
The error is exactly the same with the findBy method. Note that if jest is executed the tests pass. It is also worth mentioning that with react 17 I have not had this problem.
I worked around this by adding the following to a setupFile:
// Simulate Jest for waitFor()// see https://github.com/testing-library/dom-testing-library/blob/0ce0c7054dfa64d1cd65053790246aed151bda9d/src/helpers.ts#L5// and https://github.com/testing-library/dom-testing-library/blob/0ce0c7054dfa64d1cd65053790246aed151bda9d/src/wait-for.js#L53global.jest={advanceTimersByTime: (ms: number)=>vi.advanceTimersByTime(ms),}asany;
Doesn't seem like this is a Vitest problem. vi.advanceTimersByTime does what you asked it to do. For better compatibility with jest you can add a global jest variables in your setupFiles:
Describe the bug
Describe the bug
useFakeTimer returns timeout error when used with waitFor in react 18.
I've been trying to use
wairFor
and fake timers in a unit test but i get this error message:I created a repo in github to show this problem and here it is.
Unit test:
React component:
The error is exactly the same with the
findBy
method. Note that if jest is executed the tests pass. It is also worth mentioning that with react 17 I have not had this problem.Reproduction
https://github.com/maximinetto/vitest-issue
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: