mocking all axios instances with axios-mock-adapter

1.1k views Asked by At

Unfortunatelly, attempting to mock the whole axios package in jest test (as below) when axios.create is called somewhere doesn't work:

import axios from 'axios'
import AxiosMockAdapter from 'axios-mock-adapter'
const axiosMock = new AxiosMockAdapter(axios)

The only workaround I got so far is to export every axios instance and mock them individually from tests.

I'd want to have axios mocked globally. I have multiple axios instances created through axios.create, and ideally, I would like them to be all mocked through the same AxiosMockAdapter.

I tried playing with jest mock, by setting global.axiosMock in jest a setupFilesAfterEnv file, but I can't really see this working this way (axios package ends up being undefined anywhere it's called):

global.axiosMock = new AxiosMockAdapter(axios)
jest.mock('axios', () => global.axiosMock)
0

There are 0 answers