reactjs - 在带有 TypeScript 的 react-testing-library act() 中使用 Jest Fake 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 */
经过一番搜索,我发现了一个视频,建议将任何调用包装jest.advanceTimersByTime
在act()
function中。
act(() => jest.advanceTimesByTime(1000);
但是我正在使用 TypeScript,现在对如何解决产生的类型错误感到困惑:
TS2769:Type 'typeof jest' is not assignable to type 'void'.
如何正确修复此类型错误?
解决方案
我遇到了和你一样的问题,用大括号转换为常规函数可以防止类型错误:
act(() => {
jest.advanceTimersByTime(1000);
});
推荐阅读
- python - 不明白为什么这个 AttributeError: 'Graph' object has no attribute 'merge_one' 正在发生
- qt - 如何在页面模块上使用实体?
- c++ - 简化代码:将两个函数合并为一个 - 将结构作为参数传递?
- java - 未知属性:“-fx-background-color”
- angular - Angular 7 - Change value of received data from database
- .net - Visual Studio 2017 无法引用 System.Windows.Forms.DataVisualization
- java - 在 Java 中以十六进制生成公钥和私钥
- autowired - Symfony4 注入环境变量和自动装配错误“无法自动装配服务”
- ruby-on-rails - 使用 Angular $http.post 时会话保留过时的值?
- node.js - 为什么我在 WSL 中的 npm start 上出现 Ganache 错误?