-
Notifications
You must be signed in to change notification settings - Fork 248
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
Possible to register multiple MockAdapters? #207
Comments
+1 Would definitely like this as a feature. |
I've come across this problem too, but not directly in a testing situation, more that I'm building the front-end of an app that doesn't have a back-end yet, so I want to make something that works with the backend responses "faked". The way that I achieved this is as follows: import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
function mockAuthGet() {
const mock = new MockAdapter(axios, { delayResponse: 1000 });
mock.onGet('/api/auth').reply(({ headers }) => {
// ...
});
return () => mock.restore();
} export const fetchAuth = (token = null) => async (dispatch) => {
const restore = mockAuthGet();
const user = await axios.get('/api/auth');
await dispatch({ type: 'FETCH_AUTH_FULFILLED', user });
restore();
}; In tests you can get a similar but cleaner version by using the testing library's before/after beforeEach/afterEach or various equivalents. I do still think there should be a non-singleton instance available though. Perhaps just something like: const mock = new MockAdapter.once(axios, { delayResponse: 1000 });
// or
const mock = new MockAdapter(axios, { delayResponse: 1000, mutate: false });
// (it's a Monday morning and my naming-things-brain isn't working yet) It's also annoying having it as a singleton because I'd quite like to have a different |
This is also an issue when running tests in parallel. If a test is using a different Anyone found a way to prevent this so far? |
any fix to this? |
I think the issue is that it attaches to the axios instance, and if you use axios as a singleton, then running in parallel will cause inconsistency. I ended up introducing a lightweight dependency injection so that a separate/isolated before(() => {
http = axios.create()
httpMock = new MockAdapter(http)
})
afterEach(() => {
httpMock.reset()
})
it ('test something', () => {
// inject http into the app/component
// mock responses
// test
}) |
@GabLeRoux is that true ? My test are parallel and they work fine. |
Hey there, it's been almost 4 years now. I don't have anything to reproduce somehwere, but that shouldn't be too hard to create a blank project and reproduce this. Not sure if this is still an issue tho. |
Still an issue. |
I have a few libraries split out as sort of a monorepo. Each library has it's own mocks that I want to register, so each does a
const mockApi = new MockAdapter(axios);
however it seems thatnew MockAdapter()
replaces all previousMockAdapter
s, so only the last library included has it's mocks registered.Since my libraries are split out to different packages, there isn't an easy way to pass around a single
MockAdapter
.Is there a way to handle this?
Thanks for any help!
The text was updated successfully, but these errors were encountered: