title: How to reset global variable on custom hook
body: <p>I need to reset global variable on custom hook when unit testing React component. I have read few tutorials and StackOverflow answers to this simple task, but without luck to implement it correctly.</p>
  <p><strong>The problem</strong></p>
  <p>userInfo is undefined in the first and second test but when runs the third test userInfo is defined then on useEffect doesn't change the value... So my question is how to reset userInfo for each test.</p>
  <pre><code>jest.resetModules // doesn't work
  jest.isolateModules // doesn't work
  </code></pre>
  <p>My simplest possible setup for single test is as following:</p>
  <p><strong>My Environment</strong></p>
  <pre><code>&quot;jest&quot;: &quot;^24.9.0&quot;,
  </code></pre>
  <p><strong>My Hook</strong></p>
  <pre><code>import {useState, useEffect} from &quot;react&quot;;
  
  // This variable is an object save user info
  let userInfo = null;
  
  export default (authService) =&gt; {
    const [error, setError] = useState(null);
    const [loading, setLoading] = useState(false);
  
    useEffect(() =&gt; {
      if (userInfo !== null || authService === null) {
        return;
      }
  
      setLoading(true);
      authService
        ?.getUser()
        .then((response) =&gt; {
          userInfo = {owners: [{...response, cost_center: response.costCenter || &quot;N/A&quot;}]};
        })
        .catch(() =&gt; {
          setError({
            title: &quot;authService Error&quot;,
            message: &quot;Error getting user&quot;,
            status: 500
          });
        })
        .finally(() =&gt; setLoading(false));
    }, [authService]);
  
    return [userInfo, error, loading];
  };
  </code></pre>
  <p><strong>My Test</strong></p>
  <pre><code>import {renderHook} from &quot;@testing-library/react-hooks&quot;;
  import * as sinon from &quot;sinon&quot;;
  import {getSpyOfUseOktaAuth} from &quot;../../../__tests__/&quot;;
  import {Info, InfoFromRequest, InfoWithNoCostCenter} from &quot;../../../__tests__/&quot;;
  
  describe(&quot;useGetUserInfo&quot;, () =&gt; {
    let clock;
  
    beforeEach(() =&gt; {
      clock = sinon.useFakeTimers();
      jest.useFakeTimers();
    });
  
    afterAll(() =&gt; {
      clock.restore();
    });
  
    it(&quot;should set the error value after the getUserInfo function throws an error&quot;, async () =&gt; {
      const useGetUserInfo = require(&quot;../index&quot;).default;
  
      const errorMessage = {
        title: &quot;authService Error&quot;,
        message: &quot;Error getting user from&quot;,
        status: 500
      };
  
      const getAuthMock = getSpyOfUseAuth({
        Auth: {
          signOut: jest.fn(),
          getUser: jest.fn(async () =&gt; {
            throw new Error(&quot;Auth Error&quot;);
          })
        },
        authState: {}
      });
  
      const {result, rerender, waitForNextUpdate} = renderHook(() =&gt;
        useGetUserInfo(getAuthMock.results.Auth)
      );
  
      rerender();
  
      await waitForNextUpdate();
  
      expect(result.current[1]).toEqual(errorMessage);
      getAuthMock.instance.mockRestore();
    });
  
    it(&quot;should return the user info from after run the getUserInfo function&quot;, async () =&gt; {
      const useGetUserInfo = require(&quot;../index&quot;).default;
  
      let authService = null;
  
      const {result, rerender, waitForNextUpdate} = renderHook(() =&gt; useGetOktaUserInfo(authService));
  
      const getAuthMock = getSpyOfUseAuth({
        Auth: {
          signOut: jest.fn(),
          getUser: jest.fn(async () =&gt; Info)
        },
        authState: {}
      });
  
      authService = getAuthMock.results.Auth;
  
      rerender();
  
      await waitForNextUpdate();
  
      expect(result.current[0]).toEqual(InfoFromRequest);
      getAuthMock.instance.mockRestore();
    });
  
    it(&quot;should set cost_center as in data as N/A if costCenter is not defined in user info &quot;, async () =&gt; {
      const useGetUserInfo = require(&quot;../index&quot;).default;
  
      const getAuthMock = getSpyOfUseAuth({
        Auth: {
          signOut: jest.fn(),
          getUser: jest.fn(async () =&gt; InfoWithNoCostCenter)
        },
        authState: {}
      });
  
      const {result, rerender} = renderHook(() =&gt; useGetUserInfo(getAuthMock.results.Auth));
  
      rerender();
  
      expect(result.current[0].owners[0].cost_center).toEqual(&quot;N/A&quot;);
      getAuthMock.instance.mockRestore();
    });
  });
  </code></pre>