Aym*_*ier 4 typescript reactjs jestjs react-testing-library
我将 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 */
Run Code Online (Sandbox Code Playgroud)
经过一番搜索,我发现了一个视频,建议将任何调用包装在函数jest.advanceTimersByTime中act()。
act(() => jest.advanceTimesByTime(1000);
Run Code Online (Sandbox Code Playgroud)
然而,我正在使用 TypeScript,现在对如何解决由此产生的类型错误感到困惑:
TS2769:Type 'typeof jest' is not assignable to type 'void'.
Run Code Online (Sandbox Code Playgroud)
如何正确修复此类型错误?
小智 6
我遇到了与您相同的问题,并使用大括号转换为常规函数可以防止类型错误:
act(() => {
jest.advanceTimersByTime(1000);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5283 次 |
| 最近记录: |