axios-mock-adapter
Axios adapter that allows to easily mock requests
Installation
Using npm:
$ npm install axios-mock-adapter --save-dev
It's also available as a UMD build:
- https://unpkg.com/axios-mock-adapter/dist/axios-mock-adapter.js
- https://unpkg.com/axios-mock-adapter/dist/axios-mock-adapter.min.js
axios-mock-adapter works on Node as well as in a browser, it works with axios v0.9.0 and above.
Example
Mocking a GET
request
var axios = ;var MockAdapter = ; // This sets the mock adapter on the default instancevar mock = axios; // Mock any GET request to /users// arguments for reply are (status, data, headers)mock; axios ;
Mocking a GET
request with specific parameters
var axios = ;var MockAdapter = ; // This sets the mock adapter on the default instancevar mock = axios; // Mock GET request to /users when param `searchText` is 'John' // arguments for reply are (status, data, headers)mock; axios ;
To add a delay to responses, specify a delay amount (in milliseconds) when instantiating the adapter
// All requests using this instance will have a 2 seconds delay:var mock = axiosInstance delayResponse: 2000 ;
You can restore the original adapter (which will remove the mocking behavior)
mock;
You can also reset the registered mock handlers with reset
mock;
reset
is different from restore
in that restore
removes the mocking from the axios instance completely,
whereas reset
only removes all mock handlers that were added with onGet, onPost, etc. but leaves the mocking in place.
Mock a low level network error
// Returns a failed promise with Error('Network Error');mock;
Mock a network timeout
// Returns a failed promise with Error with code set to 'ECONNABORTED'mock;
Passing a function to reply
mock;
Using a regex
mock;
Specify no path to match by verb alone
// Reject all POST requests with HTTP 500mock;
Chaining is also supported
mock ;
.replyOnce()
can be used to let the mock only reply once
mock // After the first request to /users, this handler is removed ; // The second request to /users will have status code 500 // Any following request would return a 404 since there are // no matching handlers left
Mocking any request to a given url
// mocks GET, POST, ... requests to /foomock;
.onAny
can be useful when you want to test for a specific order of requests
// Expected order of requests:const responses = 'GET' '/foo' 200 foo: 'bar' 'POST' '/bar' 200 'PUT' '/baz' 200; // Match ALL requestsmock;
Requests that do not map to a mock handler are rejected with a HTTP 404 response. Since
handlers are matched in order, a final onAny()
can be used to change the default
behaviour
// Mock GET requests to /foo, reject all others with HTTP 500mock ;
Mocking a request with a specific request body/data
mock;
.passThrough()
forwards the matched request over network
// Mock POST requests to /api with HTTP 201, but forward// GET requests to servermock ;
Recall that the order of handlers is significant
// Mock specific requests, but let unmatched ones throughmock ;
Note that passThrough
requests are not subject to delaying by delayResponse
.
As of 1.7.0, reply
function may return a Promise:
mock;
Composing from multiple sources with Promises:
var normalAxios = axios;var mockAxios = axios;var mock = ; mock ;