question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`renderHook` result values with `waitFor` not working

See original GitHub issue

Describe the bug

Using the new renderHook function included in this library does not behave the same way when it comes to awaiting different return values as the other implementations (referencing specifically react-hooks + testing-library/react).

Trying to waitFor different values to change from the hook directly does not seem to be functioning as expected.

Expected behavior

If https://github.com/testing-library/react-hooks-testing-library is going to be deprecated in favor of this library, I’d expect to retain the same functionality the previous library offered since it was created to test complex hooks without the need for testing the components using these hooks as well. I’d also expect similar functionality to the testing-library/react implementation.

waitFor should be able to handle waiting for the values of the hook to match the expectation passed to it

Steps to Reproduce

Consider the following hook:

const useCount = () => {
  const [count, setCount] = useState(0)

  useEffect(() => {
    const retrieveCount = async () => {
      await new Promise(resolve => {
        setTimeout(() => {
          resolve(undefined)
        }, 500)
      })

      setCount(10)
    }

    retrieveCount()
  }, [])

  return count
}

If we wanted to try and waitFor count to be 10, there doesn’t appear to be a way to do this with testing-library/react-native as we can with the others:

  it('waits until count has been set', async () => {
    const { result } = renderHook(() => useCount())
   
    // this line will always fail. `result.current` is always 0, no matter the options/rerendering/etc
    await waitFor(() => expect(result.current).toBe(10))

    // this syntax will also seemingly not do anything, which worked previously in the react-hooks library
    await waitFor(() => result.current === 10)

    expect(true).toBeTrue()
  })

The above example will work just fine in testing-library/react, however.

Screenshots

Versions

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:5
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
pierrezimmermannbamcommented, Nov 2, 2022

@bkdev98 I made the following changes in your test and it now works

describe("useProductDetail", () => {
  beforeEach(() => {
    jest.useFakeTimers();
  });

  test("should instantly show initial product data", async () => {
    jest.useFakeTimers();

    const { result } = renderHook(() => useTestAsyncHook());

    await waitFor(() => expect(result.current).toBe(true), { timeout: 2500 });

    expect(result.current).toEqual(true);
  });
});

As @mdjastrzebski mentioned the callback provided to waitFor should throw when the expectation is not met and also since you have a setTimeout of 2000ms, you need to increase the waitFor timeout which is of 1000ms per default so that fake timers are advanced by 2000ms, else it will timeout after 1000s

2reactions
kylebakecommented, Sep 26, 2022

Looks like I’m no longer able to reproduce the issue with v11.2.0, thanks for all the collaboration on this! Apologies for my slow responses, I’ll try to help out more moving forward with this repo if I can

Read more comments on GitHub >

github_iconTop Results From Across the Web

WaitForNextUpdate of renderHook of react-testing-library ...
My problem is that my test gives me an error: Timeout - Async callback was not invoked within the 5000 ms ... ,...
Read more >
waitFor doesn't work if jest fake timers are used #631 - GitHub
Problem. When using waitFor when Jest has been configured to use fake ... import { renderHook } from '@testing-library/react-hooks' it('does not work', ...
Read more >
Advanced Hooks - React Hooks Testing Library
Sometimes, a hook can trigger asynchronous updates that will not be immediately reflected in the result.current value. Luckily, renderHook returns some ...
Read more >
A Complete Guide to Testing React Hooks - Toptal
With that knowledge, you will be able to debug any issues that may arise ... Since we will be testing our hook via...
Read more >
Testing | TanStack Query Docs
Install this by running: ... Note: when using React 18 or later, renderHook is available directly through the ... await waitFor(() => expect(result.current....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found