title: How to mock Axios with Jest?
body: <p>I have a function in <code>client/index.js</code> which is using axios to make a request</p>
  <pre><code>import axios from &quot;axios&quot;;
  
  const createRequest = async (url, method) =&gt; {
      const response = await axios({
          url: url,
          method: method
      });
      return response;
  };
  
  export default { createRequest };
  </code></pre>
  <p>I want to test this function using <code>jest</code>, so I created <code>client/index.test.js</code></p>
  <pre><code>import { jest } from &quot;@jest/globals&quot;;
  import axios from &quot;axios&quot;;
      
  import client from &quot;./&quot;;
  
  jest.doMock('axios', () =&gt; jest.fn(() =&gt; Promise.resolve()));
  
  describe(&quot;Client&quot;, () =&gt; {
  
      it(&quot;should call axios and return a response&quot;, async () =&gt; {
          const response = await client.createRequest('http://localhost/', 'GET');
  
          expect(axios).toHaveBeenCalled();
      })
  })
  </code></pre>
  <p>But when I try to run this, the test is failing and I am getting this error</p>
  <pre><code>connect ECONNREFUSED 127.0.0.1:80
  </code></pre>
  <p>If I use mock instead of doMock, then I am getting this error -</p>
  <pre><code>ReferenceError: /Users/project/src/client/index.test.js: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
      Invalid variable access: jest
  </code></pre>
  <p><code>package.json</code> -</p>
  <pre><code>{
      &quot;name&quot;: &quot;project&quot;,
      &quot;version&quot;: &quot;0.0.1&quot;,
      &quot;main&quot;: &quot;index.js&quot;,
      &quot;author&quot;: &quot;author&quot;,
      &quot;license&quot;: &quot;MIT&quot;,
      &quot;private&quot;: false,
      &quot;type&quot;: &quot;module&quot;,
      &quot;scripts&quot;: {
          &quot;start&quot;: &quot;node --experimental-json-modules --experimental-specifier-resolution=node ./src/index.js&quot;,
          &quot;start:dev&quot;: &quot;nodemon --experimental-json-modules --experimental-specifier-resolution=node ./src/index.js&quot;,
          &quot;test&quot;: &quot;node --experimental-vm-modules node_modules/.bin/jest&quot;,
          &quot;test:dev&quot;: &quot;node --experimental-vm-modules node_modules/.bin/jest --watch&quot;,
          &quot;test:coverage&quot;: &quot;node --experimental-vm-modules node_modules/.bin/jest --coverage&quot;,
          &quot;lint&quot;: &quot;eslint --fix .&quot;,
          &quot;pretty&quot;: &quot;prettier --write .&quot;
      },
      &quot;dependencies&quot;: {
          &quot;axios&quot;: &quot;^0.21.1&quot;,
          &quot;express&quot;: &quot;^4.17.1&quot;
      },
      &quot;devDependencies&quot;: {
          &quot;babel-eslint&quot;: &quot;^10.1.0&quot;,
          &quot;eslint&quot;: &quot;^7.23.0&quot;,
          &quot;jest&quot;: &quot;^26.6.3&quot;,
          &quot;prettier&quot;: &quot;^2.2.1&quot;,
          &quot;supertest&quot;: &quot;^6.1.3&quot;
      },
      &quot;jest&quot;: { &quot;testEnvironment&quot;: &quot;node&quot; }
  }
  </code></pre>
  <p>I am running this in node env and node version is <code>14.16.0</code>, jest version is <code>26.6.3</code>.
  Please help to identify what is wrong in this approach and how to fix it.</p>