axios-mock-adapter
axios-mock-adapter copied to clipboard
Mocking default instance mocks new instances
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.
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.
This is how axios works, I'd gladly accept a PR that adds this information to the readme.
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?
i meet TypeError: Cannot read property 'defaults' of undefined