[英]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.