繁体   English   中英

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

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

我将 Jest 与react-testing-library一起使用,并在模拟推进计时器时遇到此警告:

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

经过一番搜索,我发现了一个视频,建议在act() function 中包含对jest.advanceTimersByTime的任何调用

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

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

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

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

我遇到了和你一样的问题,并用花括号转换成常规的 function 防止类型错误:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM