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

2

18

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.

Amand answered 19/5, 2022 at 16:33 Comment(0)
G
19

An alternative could be replacing it by waitFor.

Before:

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

After

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

Grimbald answered 6/6, 2022 at 8:32 Comment(0)
C
3

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

await act(async () => {
  await waitFor(() => {
    expect(result.current.isAuthenticated).toBeUndefined();
  });
});
Chimborazo answered 12/1, 2023 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.