dom-testing-library icon indicating copy to clipboard operation
dom-testing-library copied to clipboard

How to fix warning `An update to TimerRoot inside a test was not wrapped in act(...)` with timers?

Open blasdfaa opened this issue 11 months ago • 1 comments

tests are running correctly, but a warning appears in console. How can I fix this?

Warning: An update to TimerRoot inside a test was not wrapped in act(...).

When testing, code that causes React state updates should be wrapped into act(...):

act(() => {
  /* fire events that update state */
});
/* assert on the output */

Demo - https://stackblitz.com/edit/vitejs-vite-7zbv1rkb?file=src%2FApp.test.tsx Run npm run test

blasdfaa avatar Jan 18 '25 19:01 blasdfaa

vi.advanceTimersByTime is sync timer. if u want async timer, use vi.advanceTimersByTimeAsync

await act(async () => vi.advanceTimersByTimeAsync(1000));

temm1210 avatar May 05 '25 18:05 temm1210