I’ve updated Testing-library and I’m struggling to find and alternative to waitForNextUpdate
in this case (it is no longer available in react-testing-library
v13 https://github.com/testing-library/react-testing-library/issues/1101). I’ve tried to use rerender()
it seems it is not helping:
const mockGetSummary = getSummary as jest.MockedFunction<any>;
test('initial state of hook', async () => {
mockGetSummary.mockImplementation(() => ({
setOptions: () => ({
call: () => Promise.resolve({mycustomObject}),
}),
}));
const { result, rerender } = renderHook(() => useHomePageData());
expect(result.current).toMatchObject({
loading: true,
stats: null
});
await waitForNextUpdate(); // <--- how to wait for the hook to be executed?
expect(result.current).toMatchObject({
loading: false,
stats: {...}
});
});
2
Answers
In newer versions of React Testing Library,
waitForNextUpdate()
is no longer available. Instead, you can handle the asynchronous updates using the act function, which is also provided by@testing-library/react-hooks
.try this code:
Using
act
ensures that the component updates are handled correctlyI can find a related Github topic here and the answer of eps1lon, member of testing librariry, was:
waitFor
docs