axios-mock-adapter: Axios.create is not a function

Hi @ctimmerm,

I’m having an “axios.create() is not a function error” when i try to use axios-mock-adapter. Here’s the implementation. `

      import axios from 'axios';
      import MockAdapter from 'axios-mock-adapter';

      let mock = new MockAdapter(axios);
       mock.onGet('https://myapi.org/v1/', { params: { param1: 'holiday', sortBy:'top' } }).reply(200, {
		  	data: [{
				     author: "Vivian Michaels",
				}]
                              });

      axios.get('https://myapi.org/v1/', { params: { param1: 'holiday', sortBy:'top' } })
		  .then(function(response) {
		    console.log(response.data);
		 });`

I’m doing exactly as the documentation says but then i get the error: TypeError: axios.create is not a function. Any help is appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 30
  • Comments: 15 (1 by maintainers)

Most upvoted comments

@StanSarr im just mocking it via jest mock, i have created file axios.js in __mocks__ folder and jest mock it automatically, then you can import axios in you tests and spy on it or make another mock implementation to test different responses.

const axios = {
  get: jest.fn(() => Promise.resolve({ data: {} })),
  create: () => axios,
  defaults: {
    adapter: {},
  },
};

export default axios;

edit: i didnt managed to make it work with this lib, even when i removed jest mock. only reason i was looking for alternative is that i didnt notice that someone who wrote this jest mock didnt use jest.fn() and i thought i cant mock any responses.

Adding this here since it’s the first hit on google to the question.

With jest, you can explicitly unmock, then call this library.

jest.unmock('axios');
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
...

This library gives nice, flexible apis when working with external requests. However it doesn’t globally prevent your app from making external calls that can be triggered by a test in a different component.

So I found both using this library and doing a manual mock in the __mocks__ folder equally helpful.

For those who might want to automock axios, this works.

// __mocks__/axios.js

// easier to merge objects
const _ = require('lodash');

// cannot require axios, otherwise it recursively mocks
const axios = jest.requireActual('axios');
jest.unmock('axios');

const MockAdapter = require('axios-mock-adapter');
const mockAxios = new MockAdapter(axios);

// nothing important seems to be overwritten
module.exports = _.assignIn(axios, mockAxios);

Usage

// index.test.js
const axios = require('axios');

axios.onGet( ... )

// index.js
axios.get( ... )

This library is useless without create instance.

This is still a problem for me with Jest 😞. Some documentation on how to set this package up with Jest’s __mocks__ style folder would be really nice.

hey @Gecko222 , How did you fix the problem ?

Following up on Oscar’s answer above, if you need to mock an instance created with axios.create() this seems to be working for me:

// easier to merge objects
const _ = require('lodash');

// cannot require axios, otherwise it recursively mocks
const axios = jest.requireActual('axios');
jest.unmock('axios');

const MockAdapter = require('axios-mock-adapter');
const mockAxios = new MockAdapter(axios);

// nothing important seems to be overwritten
module.exports = _.assignIn(axios, mockAxios, { create:(options)=>axios });// override create to return the mocked axios default instance.

**One caveat: ** calling reset() or resetHandlers() causing everything to go haywire so cleanup is not possible in my experience, but other than not cleaning up mocks, everything works and all my tests pass and coverage is on point.

Hi @shahzadcacf , as mentioned in my last comment above, you don’t really need this library. I also write about it here

@Gecko222 Thanks a lot !

we had kind of the same error cus another test did overwrite axios.create with a stub which got not correctly reset.