简体   繁体   English

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

[英]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:我将 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 */

After some searching, I found a video suggesting wrapping any calls to jest.advanceTimersByTime in the act() function .经过一番搜索,我发现了一个视频,建议在act() function 中包含对jest.advanceTimersByTime的任何调用

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

However I am using TypeScript and am now confused as to how to resolve the resultant type error:但是我正在使用 TypeScript 并且现在对如何解决产生的类型错误感到困惑:

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

How do I fix this type error correctly?如何正确修复此类型错误?

I met the same problem as you and converting to regular function with curly braces prevent the type error:我遇到了和你一样的问题,并用花括号转换成常规的 function 防止类型错误:

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

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

相关问题 Jest + react-testing-library:警告更新未包含在 act() 中 - Jest + react-testing-library: Warning update was not wrapped in act() Jest & React & Typescript & React-Testing-Library 错误 - Jest & React & Typescript & React-Testing-Library error 使用带有反应测试库的玩笑来测试对象的属性时出错 - Error in testing the properties of object using jest with react-testing-library 使用 react-testing-library 和 jest 测试是否调用了 prop 函数 - Testing if a prop function was called using react-testing-library and jest 如何使用 react-testing-library 和 jest 测试链接 - How to test link using react-testing-library and jest 更新未包含在 act(...) 中,并且模拟 function 未触发。 反应测试库,开玩笑 - Update not wrapped in act(…) and the mock function not firing. react-testing-library, jest 带有 jest 和 react-testing-library 的测试方法 - test method with jest and react-testing-library 使用 jest 和 react-testing-library 测试反应上下文 - Testing react context with jest and react-testing-library 使用 jest 和 react-testing-library 测试连接的组件 - Testing connected components with jest and react-testing-library 使用 Jest + react-testing-library 测试异步 `componentDidMount()` - Testing async `componentDidMount()` with Jest + react-testing-library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM