3

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)
Syffys
  • 570
  • 5
  • 21
  • Does this answer your question? [Mock inner axios.create()](https://stackoverflow.com/questions/51393952/mock-inner-axios-create) – setec Jul 10 '23 at 15:31

0 Answers0