When running a test using jest I have the basic test suit syntax:
jest.mock('axios');
describe('app', () => {
let render
beforeEach(() => {
axiosMock.get.mockResolvedValueOnce({
data: {greeting: 'hello there'},
}),
render= renderApp()
});
test('should render something', () => {
expect(something).toBeInTheDocument();
});
});
The problem is I have interceptors in my code which when running the test with jest command outputs:
TypeError: Cannot read property 'interceptors' of undefined
and points to the interceptors object
axiosInstance.interceptors.request.use(...
axiosInstance
is the a variable storing the return of axios.create
export const axiosInstance = axios.create({...
Refered to this axios thread on SO How do I test axios in jest but it doesn't involve any interceptors so didn't really help.
This was enough in the end, plain and simple jest.fn()
jest.mock('axios', () => {
return {
interceptors: {
request: { use: jest.fn(), eject: jest.fn() },
response: { use: jest.fn(), eject: jest.fn() },
},
};
});