Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jest Fake Timer in react-testing-library act() with TypeScript

I am using Jest along with react-testing-library, and encountered this warning when simulating advancing the timer:

 console.error
      Warning: An update to TimerProvider 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 */

After some searching, I found a video suggesting wrapping any calls to jest.advanceTimersByTime in the act() function.

act(() => jest.advanceTimesByTime(1000);

However I am using TypeScript and am now confused as to how to resolve the resultant type error:

TS2769:Type 'typeof jest' is not assignable to type 'void'.

How do I fix this type error correctly?

like image 349
Aymon Fournier Avatar asked Sep 04 '25 16:09

Aymon Fournier


1 Answers

I met the same problem as you and converting to regular function with curly braces prevent the type error:

act(() => {
  jest.advanceTimersByTime(1000);
});

like image 184
Ozgun Bal Avatar answered Sep 07 '25 12:09

Ozgun Bal