-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to mock axios instances #166
Comments
hey @ingro did you ever figure out a better way to handle this use case? I'm trying to test instances of axios too as I wanted to have global interceptors to handle the errors. |
Hey, I thought I was running into a similar problem, but the following code works for me: // httpServices.js
export const api = axios.create() and // httpServices.test.js
import {
api
} from './httpServices'
describe('httpServices tests', () => {
beforeAll(() => {
mock = new MockAdapter(api)
})
}) Hope that helps! |
Thanks! and yeah I found out it was working for me too but I was calling it with the wrong URL formatting in the mock/test. |
I ran into this issue because of global interceptors as well :) |
how do you test it you're calling the client constructor directly: const config = {
method: 'GET',
url: 'some/url/here',
};
await client(config));
|
I am having this issue as well. I can mock axios if I am calling Test:
myFunction(input)
Expected: axios uses Same thing happens with |
@single-stop-justin did you solve your problem? I am running into the same issue :( |
What if I want to mock all instances in my test file? Seems really annoying to extract all instances, and mock accordingly their requests |
Hello and thanks for this library!
I have however a problem when trying to test requests made by instances created with
axios.create()
.I have this client file that I can import in my application where needed with some default settings for the instance (I actually have many of them since I'm using various API in my application):
When I try to test the component the call to the api is not intercepted by the mock and the real API is hit, causing errors in the test.
But if I export
axios
instead ofclient
from client.js the tests run just fine.So my guessing is that the mock can only be applied to the default instance of axios, and not on subsequently created instances. Is my guess correct? The only workaround I found is to add a condition to the default export in client.js:
but that's not ideal either because I may want to actually hit the API on integration tests.
Are there better ways to handle this problem? Is an improvement that can be implemented on the library in the future? Thanks.
The text was updated successfully, but these errors were encountered: