question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Axios.create is not a function

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

35reactions
jgradzkicommented, Jun 19, 2018

@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.

19reactions
innocentamadicommented, Apr 28, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why TypeError: axios.create is not a function? When testing ...
This problem typically happens when you are already mocking axios (very likely within a __mocks__ folder. With jest, you can explicitly unmock, ...
Read more >
axios.create is not a function · Issue #5052 - GitHub
Hello to you, I am creating this ticket because I have a problem with the latest version of your package released a few...
Read more >
How to use the axios.default function in axios - Snyk
To help you get started, we've selected a few axios.default examples, based on popular ways it is used in public projects.
Read more >
Why TypeError: axios.create is not a function ... - iTecNote
I'm trying to test my axios API functions in React. Found this question here: how do i test axios in jest which pointed...
Read more >
Understanding Axios.create - LogRocket Blog
Axios.create is a handy feature within Axios used to create a new instance with a custom configuration. With Axios.create, we can generate a ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found