axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

Mocking default instance mocks new instances

Open TrueWill opened this issue 7 years ago • 4 comments

Mocking the default instance of axios (new MockAdapter(axios)) will affect any axios instances subsequently created (axios.create()).

I'm not sure that I'd call this a bug, but it's definitely surprising behavior; perhaps documenting it would be sufficient.

TrueWill avatar Dec 06 '17 21:12 TrueWill

I encountered a problem due to this recently. The situation is that I send two requests at once while mocking only the first one. I've written a wrapper function to axios request, basically call restore after response. However, the second one is also mocked, and it takes me an afternoon to find out the problem 😅

This problem is solved by ensuring that every axios request is a new instance.

donrsh avatar Feb 10 '18 16:02 donrsh

This is how axios works, I'd gladly accept a PR that adds this information to the readme.

ctimmerm avatar Feb 11 '18 11:02 ctimmerm

for me, I am facing the issue opposite. I have called this (new MockAdapter(axios)) at the very beginning of my test. and inside several layers inside my codes, I called const instance = axios.create({ method: 'get', baseURL: '', timeout: 0, responseType: 'json' }) and the request is sent by this new instance. I have already mocked many APIs, however, I can't get the result as I have filled. I need do more things?

ReedCao avatar Jun 10 '19 03:06 ReedCao

i meet TypeError: Cannot read property 'defaults' of undefined

hitao123 avatar May 27 '20 12:05 hitao123