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

Example of mocking response with interceptor #40

Closed
aaronshaf opened this issue Jan 28, 2015 · 9 comments · May be fixed by baby636/axios#12, baby636/axios#13, baby636/axios#14 or baby636/axios#16
Closed

Comments

@aaronshaf
Copy link

I would like to intercept a request and then pre-emptively resolve with a response (compare https://github.com/trek/pretender). Is there an example of this being done elsewhere?

@aaronshaf
Copy link
Author

From what I can tell, this isn't possible with the current version of axios. An interceptor needs some way of preempting dispatchRequest.

@mzabriskie
Copy link
Member

Is this to accommodate a test scenario?

@aaronshaf
Copy link
Author

Yes, that and caching

On Mon, Feb 2, 2015 at 9:05 PM, Matt Zabriskie notifications@github.com
wrote:

Is this to accommodate a test scenario?


Reply to this email directly or view it on GitHub
#40 (comment).

@mzabriskie
Copy link
Member

With version 0.6.0 you will be able to specify a custom adapter, which facilitates testing. There will also be built in caching of GET requests.

There's also this neat lib that is inspired by pretender that you could use https://github.com/aaronshaf/xhr-interceptor :)

@resistdesign
Copy link

@mzabriskie How do you specify a custom adapter now?

@mzabriskie
Copy link
Member

@resistdesign functionality didn't make the cut for 0.6.0. Will work on this for a future release.

@sekoyo
Copy link

sekoyo commented Nov 24, 2015

Any update on this? I much prefer axios's Promise api than superagent's callback api but I would need a way of mocking to adopt it. Thanks

@aeirola
Copy link

aeirola commented Jan 11, 2016

You can do this with axios' interceptors by failing each request, but resolving the promise in the response error handler.

// Add request interceptor to force the request to fail
axios.interceptors.request.use((config) => {
  // Mangle URL to skip network request
  config.url = 'http://localhost:0/' + config.url;
  return config;
});

instance.interceptors.response.use((response) => {
  // Shouldn't really be called, ever
  return response;
}, (error) => {
  /* Returning a resolved promise from error handler
     will make final promise resolve as though the request
     would have returned successfully */
  return Promise.resolve({
    data: {/* response data */},
    status: 200,
    statusText: 'OK',
    headers: {},
    config: error.config
  });
});

@mzabriskie
Copy link
Member

For anyone looking to address mocking for a test scenario I have created moxios.

@axios axios locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.