I am using Jest along with react-testing-library, and encountered this warning when simulating advancing the timer:
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(() => jest.advanceTimesByTime(1000);
However I am using TypeScript and am now confused as to how to resolve the resultant type error:
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:
act(() => {
jest.advanceTimersByTime(1000);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With