I am new to unit testing and jest, and was stuck at a scenario where the react’s state are being set asynchronously after API call is resolved or rejected. I found some answers in which they’ve mentioned to use waitFor
or await act(async()=>{})
. Whenever I’m trying to await with act or even async act, I’m getting this error and the tests are failing. I’ve tried to replicate a similar scenario below.
If these ways of handling async updates to component are not correct, then what could be the other solutions. I’m using react hooks to set the state after api resolves or rejects.
React and react-dom version : 16.8
test('new ', async () => {
await act(() => {
console.log('test');
});
expect('temp').toBe('temp');
});