title: Unit testing for Custom hooks with functions that contain setTimout()
body: <p>The main problem is that I can't seem to get to the next lines of the code because of an await function in my CustomHook. For context, I have created a function called sleep that looks like this.</p>
  <pre><code>export const sleep = (ms: number): Promise&lt;void&gt; =&gt;
    new Promise&lt;void&gt;((res) =&gt; setTimeout(res, ms));
  </code></pre>
  <p>Now my custom hook looks something like this.</p>
  <pre><code>const useCustomHook = () =&gt; {
    const [sampleState, setSampleState] = useState(null);
  
    const callApiFunction = async () =&gt; {
      const results = await callApi();
      await sleep(500);
      if(results.success) {
        const secondResults = await callSecondApi();
        await sleep(500); 
        if(secondResults.success) {
          setSampleState(secondResults.data); //Should set state to an {}
        }
      }
    }
  
    return {
      sampleState, 
      callApiFunction,
    }
  }
  </code></pre>
  <p>To create a unit test for this, I'm using <code>@testing-library/react-hooks</code>.</p>
  <p>I've written a test where</p>
  <pre><code>it('should do something', async () =&gt; {
    jest.useFakeTimers()
    const {result, waitForNextUpdate} = renderHook(()=&gt; useCustomHook())
    // Using act since we won't allow running functions that changes states without act
    act(() =&gt; {
      result.current.callApiFunction();
      jest.advanceTimersByTime(500);
    })
    await waitForNextUpdate()
    expect(result.current.sampleState).toEqual(expectedApiResults);
  })
  </code></pre>
  <p>The ones above are pretty much simplified but its basically the gist. I've seen other implementations but most of them are using <code>useEffect</code> which is a bit different from what I want to do.</p>
  <p>Also with he implementation above I get <em>&quot;Exceeded timeout of 5000 ms for a test. Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test.&quot;</em> and state is still `null</p>