在带有TypeScript的react-testing-library act()中使用Jest Fake Timer

时间:2020-07-07 01:29:31

标签: reactjs typescript jestjs react-testing-library

我正在与react-testing-library一起使用Jest,并且在模拟定时器的前进时遇到了以下警告:

 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 */

搜索后,我发现了a video suggesting wrapping any calls to jest.advanceTimersByTime in the act() function

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

但是我正在使用TypeScript,现在对于如何解决由此产生的类型错误感到困惑:

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

如何正确修复此类型错误?

1 个答案:

答案 0 :(得分:0)

我遇到了与您相同的问题,并使用花括号将其转换为常规函数可以防止类型错误:

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