title: Testing custom hook with SetTimeout and useEffect with Jest
body: <p>I'm trying to test a relatively simple custom hook that uses useEffect and setTimeout. However, my test fails, and I cannot figure out what is wrong.</p>
  <p>Here is the hook itself(useTokenExpirationCheck.ts)</p>
  <pre><code>import { useEffect } from 'react';
  import { logout } from '../features/profile/profileSlice';
  import { useAppDispatch } from '../store/hooks';
  
  export default function useTokenExpirationCheck(exp: number): void {
    const dispatch = useAppDispatch();
    useEffect(() =&gt; {
      if (exp) {
        const timeToLogout = exp * 1000 - Date.now();
        setTimeout(() =&gt; {
          dispatch(logout());
        }, timeToLogout);
      }
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [exp]);
  }
  </code></pre>
  <p>and my testing file:</p>
  <pre><code>import { act, renderHook } from '@testing-library/react-hooks';
  import useTokenExpirationCheck from './useTokenExpirationCheck';
  
  jest.mock('../features/profile/profileSlice');
  const logout = jest.fn();
  const exp = Date.now() + 6000;
  
  describe('Expiration token', () =&gt; {
    test('should logout user', async () =&gt; {
      jest.useFakeTimers();
      act(() =&gt; {
        renderHook(() =&gt; {
          useTokenExpirationCheck(exp);
        });
      });
      expect(logout).toHaveBeenCalledTimes(0);
      jest.advanceTimersByTime(60000);
      expect(logout).toHaveBeenCalledTimes(1);
    });
  });
  </code></pre>
  <p>What I know is that the exp variable is somehow not passed to the useTokenExpirationCheck function (console.log showed me 0 inside of the function, when it was executed). So basically, I am not even getting to the useEffect itself... any ideas what could go wrong?</p>