title: Mock a TypeScript constant different in every Jest test
body: <p>I'm struggling to mock a constant with Jest on a per test basis. I have it working with the code below, but the mock is &quot;static&quot; - I can't mock it differently for each test.</p>
  <p>Code:</p>
  <pre class="lang-js prettyprint-override"><code>// allowList.ts
  export const ALLOW_LIST = {
    '1234': true
  };
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>// listUtil.ts
  import { ALLOW_LIST } from './allowList.ts';
  
  export const checkList = (id: string) =&gt; {
    if (ALLOW_LIST[id]) return true;
    return false;
  };
  </code></pre>
  <p>Test (working):</p>
  <pre class="lang-js prettyprint-override"><code>// listUtil.test.ts
  import { checkList } from './listUtil';
  
  jest.mock('./listUtil', () =&gt; {
    return {
      '5678': true
    };
  });
  
  test('in list', () =&gt; {
    expect(checkList('5678')).toBe(true);
  });
  test('not in list', () =&gt; {
    expect(checkList('1234')).toBe(false);
  });
  </code></pre>
  <p>What I would like (not working):</p>
  <pre class="lang-js prettyprint-override"><code>// listUtil.test.ts
  import { checkList } from './listUtil';
  
  test('in list', () =&gt; {
    jest.mock('./listUtil', () =&gt; {
      return {
        '5678': true
      };
    });
    expect(checkList('5678')).toBe(true);
  });
  test('not in list', () =&gt; {
    jest.mock('./listUtil', () =&gt; {
      return {
        '9123': true
      };
    });
    expect(checkList('1234')).toBe(false);
  });
  </code></pre>
  <p>Is what I'm trying to do possible? <a href="https://stackoverflow.com/questions/45006254/jest-mock-module-per-test">This post</a> is very similar and appears to work when mocking functions, but I'm having the same issue as the commenters of the accepted answer. I think I'm just not understanding how Jest performs mocking under the hood. I believe the working version works because the mock is hoisted and basically overwrites the real implementation, but I'm not sure how or if I can achieve that in each test.</p>
  <p>I think one option would be to expose the ALLOW_LIST via a function:</p>
  <pre class="lang-js prettyprint-override"><code>// allowList.ts
  const ALLOW_LIST = {
    '1234': true
  };
  export const getAllowList = () =&gt; ALLOW_LIST;
  </code></pre>
  <p>and mock that, but am wondering if that's necessary.</p>