title: 'ReferenceError: jest is not defined' when running unit test
body: <p>I'm in the early stages of a new app that so far just uses vanilla JS. I'm trying to use ES6 modules for my Jest unit tests, so I followed the <a href="https://stackoverflow.com/a/61652773">2020 updated instructions</a> on making this possible.</p>
  <p>However, after following those instructions, I get the error <code>ReferenceError: jest is not defined</code> when running my unit test.</p>
  <p>Here's my package.json:</p>
  <pre><code>{
    &quot;version&quot;: &quot;1.0.0&quot;,
    &quot;main&quot;: &quot;app.js&quot;,
    &quot;type&quot;: &quot;module&quot;,
    &quot;jest&quot;: {
      &quot;testEnvironment&quot;: &quot;jest-environment-node&quot;,
      &quot;transform&quot;: {}
    },
    &quot;scripts&quot;: {
      &quot;test&quot;: &quot;node --experimental-vm-modules node_modules/jest/bin/jest.js&quot;,
      &quot;start&quot;: &quot;node server.js&quot;
    },
    &quot;license&quot;: &quot;ISC&quot;,
    &quot;devDependencies&quot;: {
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;jest&quot;: &quot;^26.6.3&quot;,
      &quot;jest-environment-node&quot;: &quot;^26.6.2&quot;,
      &quot;open&quot;: &quot;^7.3.0&quot;
    },
    &quot;dependencies&quot;: {}
  }
  </code></pre>
  <p>Below is my test file. The error occurs due to the jest.fn call:</p>
  <pre><code>import { getTotalNumPeople } from &quot;./app.js&quot;;
  
  
  test(&quot;Get total number of people&quot;, async () =&gt; {
      global.fetch = jest.fn(() =&gt; Promise.resolve({
              json: () =&gt; Promise.resolve({count: 15})
          })
      )
      
      expect(await getTotalNumPeople()).toBe(15);
  });
  </code></pre>
  <p>Any ideas on why this is happening? I'm confident that the issue has to do with the steps I followed to support ES6 modules. Prior to these changes, my test ran fine when I simply pasted the <code>getTotalNumPeople</code> function in my test file.</p>
  <p>If I comment out mocking my function, I then get a ReferenceError about fetch not being defined (since <code>getTotalNumPeople</code> uses fetch). So it's not just <code>jest</code> that's not defined.</p>
  <p>I notice that if I do not specify <code>jest-environment-node</code> as my test environment, the error changes to <code>ReferenceError: global is not defined</code> due to referring to <code>global.fetch</code> in my test. Just thought I'd note that in case that helps.</p>