mock custom instance of axios in vue jest test

Viewed 1034

The Problem

I have a custom axios instance at src/axios:

import axios from 'axios';

 const instance = axios.create({
   baseURL: 'myApi/',
 });

export default instance;

In MyComponent I import axios with import axios from '../axios' and make an api call.

In testing the api call in MyComponent I must mock axios so I do:

import axios from 'axios'; jest.mock('axios');

but when I run my test axios is undefined inside MyComponent and the test fails.

What I Tried

  1. importing the custom axios instance into the test gives the same error, axios is undefined.
  2. in MyComponent I import axios from node_modules eg import axios from 'axios' then the test works but I can't use my custom axios instance.

Vue 2.6, Jest @vue/cli-plugin-unit-jest 4.2.3

How can I use the custom instance in MyComponent and mock it in the test?

0 Answers
Related