I'm trying to use mockAxios for testing with axios interceptors.
export default {
get: jest.fn(() => Promise.resolve({ data: {} }))
}
import axios from 'axios';
export const configurateAxios = () => {
axios.interceptors.response.use(
response => {
return response;
},
error => {
return Promise.reject(error);
}
);
}
When I created mockAxios:
export default {
get: jest.fn(() => Promise.resolve(data: {}))
}
All of my tests failed with the follow message: cannot read property response of undefined inside of axios interceptors. It happens because mock axios doesn't return response. It could just return a plain object.
So how can I use axios interceptors with mockAxios for testing?
Why don't you just mock the axios
get()
method using standard jest mocking?This is how I would go about it:
After this, all calls to axios'
get
should be mocked according to youraxiosMockedGet
implementation.This how I achieved
Interceptor.js
Interceptor.test.js