Skip to content
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

Open
rally25rs opened this issue Jun 19, 2019 · 8 comments
Open

Possible to register multiple MockAdapters? #207

rally25rs opened this issue Jun 19, 2019 · 8 comments

Comments

@rally25rs
Copy link

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 that new MockAdapter() replaces all previous MockAdapters, 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!

@gburning
Copy link

+1

Would definitely like this as a feature.

@MaffooBristol
Copy link

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 delayResponse per request, but this is globally defined.

@GabLeRoux
Copy link

This is also an issue when running tests in parallel. If a test is using a different MockAdapter, it will be used for all other tests while it's being set. The reference is shared, causing other tests to use the wrong MockAdapter.

Anyone found a way to prevent this so far?

@jesalazaro
Copy link

any fix to this?

@Mihailoff
Copy link

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 axios instance with attached mock adapter is used by the application in testing env.

before(() => {
  http = axios.create()
  httpMock = new MockAdapter(http)
}) 

afterEach(() => {
  httpMock.reset()
})

it ('test something', () => {
  // inject http into the app/component
  // mock responses
  // test  
})

@Amerr
Copy link

Amerr commented Nov 2, 2023

@GabLeRoux is that true ?

My test are parallel and they work fine.
Could you post a repo to reproduce it

@GabLeRoux
Copy link

GabLeRoux commented Nov 5, 2023

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.

@AaronTrazona
Copy link

Still an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants