Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the alternative the function waitForNextUpdate (@testing-library/react-hooks) in @testing-library/react for async hooks?

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.

like image 459
Ronald Castillo Avatar asked Dec 08 '25 06:12

Ronald Castillo


2 Answers

An alternative could be replacing it by waitFor.

Before:

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

After

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

like image 150
Laurent Avatar answered Dec 09 '25 20:12

Laurent


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

await act(async () => {
  await waitFor(() => {
    expect(result.current.isAuthenticated).toBeUndefined();
  });
});
like image 21
Pedro Arantes Avatar answered Dec 09 '25 20:12

Pedro Arantes