title: Can't mock axios instance in Typescript
body: <p>Following <a href="https://binarapps.com/blog/test-ajax-calls-in-react-component-lifecycle" rel="nofollow noreferrer">this link</a>, I tried writing a unit test while mocking Axios (using Typescript).</p>
  
  <p>Using an Axios instance to set <code>baseUrl</code>.</p>
  
  <pre><code>// src/infrastructure/axios-firebase.ts
  import axios from 'axios';
  
  const axiosThroughFirebase = axios.create({
      baseURL: 'firebase_URL'
  });
  
  export default axiosThroughFirebase;
  </code></pre>
  
  <p>Here's the abridged version of the component I want to test.</p>
  
  <pre><code>// src/container/MainContainer/MainContainer.tsx
  import axios_firebase from '../../infrastructure/axios-firebase';
  ...
  
  public componentDidMount() {
      axios_firebase.get('firebase_url/data.json')
         .then(resp =&gt; this.setState({ stuff }));
  }
  </code></pre>
  
  <p>And then my test file.</p>
  
  <pre><code>// src/container/MainContainer/MainContainer.test.tsx
  jest.mock('../../infrastructure/axios-firebase', () =&gt; {
      return {
         get: jest.fn(() =&gt; Promise.resolve(someFakeData))
      };
  });
  
  import axios_firebase from '../../infrastructure/axios-firebase';
  
  test('fetches data on componentDidMount', async () =&gt; {
      const wrapper = enzyme.shallow(&lt;MainContainer /&gt;);
      wrapper.instance().componentDidMount()
         .then(() =&gt; {
             expect(axios_firebase.get).toHaveBeenCalled();
         });
  });
  </code></pre>
  
  <p>When I run this test, I get the following failure message:</p>
  
  <pre><code>● MainContainer › encountered a declaration exception
  
  TypeError: Cannot read property 'get' of undefined
  
    14 |
    15 |     public componentDidMount() {
  &gt; 16 |         axios_firebase.get('url_to_data')
  </code></pre>
  
  <p>I thought I was following the initial link exactly.. I can't imagine that using Typescript is the problem? I can't see why the Axios instance, which was mocked out, would be undefined in the code.</p>