14

I want to test my custom hook but in React 18 @testing-library/react-hooks library is not working, instead I am using @testing-library/react it has renderHook function and it works fine, but this library does not have waitForNextUpdate function for asynchronous hooks. For this reason, I can't test my custom async hooks.

Ronald Castillo
  • 141
  • 1
  • 4

2 Answers2

12

An alternative could be replacing it by waitFor.

Before:

await waitForNextUpdate();
expect(fetch).toHaveBeenCalledTimes(1)

After

await waitFor(() => {
   expect(fetch).toHaveBeenCalledTimes(1)
}

Laurent
  • 662
  • 2
  • 10
  • 13
2

It only worked for me when I used act and waitFor:

await act(async () => {
  await waitFor(() => {
    expect(result.current.isAuthenticated).toBeUndefined();
  });
});
Pedro Arantes
  • 5,113
  • 5
  • 25
  • 60