问题
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
- importing the custom axios instance into the test gives the same error,
axiosis undefined. - in
MyComponentI importaxiosfromnode_modulesegimport axios from 'axios'then the test works but I can't use my customaxiosinstance.
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?
来源:https://stackoverflow.com/questions/60745903/mock-custom-instance-of-axios-in-vue-jest-test