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

Ability to switch mocking on and off #43

Closed
exnihilo-creatio opened this issue Mar 24, 2017 · 4 comments
Closed

Ability to switch mocking on and off #43

exnihilo-creatio opened this issue Mar 24, 2017 · 4 comments

Comments

@exnihilo-creatio
Copy link

I would be nice to have the ability to turn mocking on during development. The library currently has the method reset, which removes the handlers, and restore which removes mocking from axios. However, there is currently no possiblity to stop mocking, remember the added handlers and start mocking again. Think the opposite of restore, but on an already initialized adapter.

Or it's already possible and I can't figure out why.

@ctimmerm
Copy link
Owner

All that axios-mock-adapter does is setting an adapter for the axios instance that you pass to it.
There's no built in support for toggling the mock adapter on and off, but it's pretty easy to achieve anyway:

var mock = new MockAdapter(axios);

// Save a reference to the mock adapter so that we can later reuse it
var mockAdapter = axios.defaults.adapter;

// Removing mocking behavior
mock.restore();

// Add mocking behavior again
axios.defaults.adapter = mockAdapter;

@exnihilo-creatio
Copy link
Author

Indeed, it is easy to achieve, I just thought maybe having a built in support might be helpful, since its not a lot of code and won't have any effect on existing code. It's a nice to have kinda feature.

@ghost
Copy link

ghost commented Feb 8, 2018

This would be as trivial to do if there is an option or if there's not:

if (process.env.NODE_ENV === 'development') {
  // Mocking code here
}

If it was as an option it'd be basically the same, let's say the option was called active:

const mock = new MockAdapter(axios, {
  active: process.env.NODE_ENV === 'development'
});

So IMHO it doesn't make sense to implement it.

@ctimmerm ctimmerm closed this as completed Jan 2, 2019
@xiaoyouyu
Copy link

All that axios-mock-adapter does is setting an adapter for the axios instance that you pass to it.
There's no built in support for toggling the mock adapter on and off, but it's pretty easy to achieve anyway:

var mock = new MockAdapter(axios);

// Save a reference to the mock adapter so that we can later reuse it
var mockAdapter = axios.defaults.adapter;

// Removing mocking behavior
mock.restore();

// Add mocking behavior again
axios.defaults.adapter = mockAdapter;

very good

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

3 participants