🌜
🌞
axios-mock-adapter

axios-mock-adapter

v1.21.1

Axios adapter that allows to easily mock requests

npm install axios-mock-adapter

README

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:

axios-mock-adapter works on Node as well as in a browser, it works with axios v0.17.0 and above.

Example

Mocking a GET request

var axios = require("axios");
var MockAdapter = require("axios-mock-adapter");

// This sets the mock adapter on the default instance
var mock = new MockAdapter(axios);

// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet("/users").reply(200, {
  users: [{ id: 1, name: "John Smith" }],
});

axios.get("/users").then(function (response) {
  console.log(response.data);
});

Mocking a GET request with specific parameters

var axios = require("axios");
var MockAdapter = require("axios-mock-adapter");

// This sets the mock adapter on the default instance
var mock = new MockAdapter(axios);

// Mock GET request to /users when param `searchText` is 'John'
// arguments for reply are (status, data, headers)
mock.onGet("/users", { params: { searchText: "John" } }).reply(200, {
  users: [{ id: 1, name: "John Smith" }],
});

axios
  .get("/users", { params: { searchText: "John" } })
  .then(function (response) {
    console.log(response.data);
  });

When using params, you must match all key/value pairs passed to that option.

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 = new MockAdapter(axiosInstance, { delayResponse: 2000 });

You can restore the original adapter (which will remove the mocking behavior)

mock.restore();

You can also reset the registered mock handlers with resetHandlers

mock.resetHandlers();

You can reset both registered mock handlers and history items with reset

mock.reset();

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.onGet("/users").networkError();

// networkErrorOnce can be used to mock a network error only once
mock.onGet("/users").networkErrorOnce();

Mock a network timeout

// Returns a failed promise with Error with code set to 'ECONNABORTED'
mock.onGet("/users").timeout();

// timeoutOnce can be used to mock a timeout only once
mock.onGet("/users").timeoutOnce();

Passing a function to reply

mock.onGet("/users").reply(function (config) {
  // `config` is the axios config and contains things like the url

  // return an array in the form of [status, data, headers]
  return [
    200,
    {
      users: [{ id: 1, name: "John Smith" }],
    },
  ];
});

Passing a function to reply that returns an axios request, essentially mocking a redirect

mock.onPost("/foo").reply(function (config) {
  return axios.get("/bar");
});

Using a regex

mock.onGet(/\/users\/\d+/).reply(function (config) {
  // the actual id can be grabbed from config.url

  return [200, {}];
});

Using variables in regex

const usersUri = "/users";
const url = new RegExp(`${usersUri}/*`);

mock.onGet(url).reply(200, users);

Specify no path to match by verb alone

// Reject all POST requests with HTTP 500
mock.onPost().reply(500);

Chaining is also supported

mock.onGet("/users").reply(200, users).onGet("/posts").reply(200, posts);

.replyOnce() can be used to let the mock only reply once

mock
  .onGet("/users")
  .replyOnce(200, users) // After the first request to /users, this handler is removed
  .onGet("/users")
  .replyOnce(500); // 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 /foo
mock.onAny("/foo").reply(200);

.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 requests
mock.onAny().reply((config) => {
  const [method, url, ...response] = responses.shift();
  if (config.url === url && config.method.toUpperCase() === method)
    return response;
  // Unexpected request, error out
  return [500, {}];
});

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 500
mock.onGet("/foo").reply(200).onAny().reply(500);

Mocking a request with a specific request body/data

mock.onPut("/product", { id: 4, name: "foo" }).reply(204);

Using an asymmetric matcher, for example Jest matchers

mock
  .onPost(
    "/product",
    { id: 1 },
    expect.objectContaining({
      Authorization: expect.stringMatching(/^Basic /),
    })
  )
  .reply(204);

Using a custom asymmetric matcher (any object that has a asymmetricMatch property)

mock
  .onPost("/product", {
    asymmetricMatch: function (actual) {
      return ["computer", "phone"].includes(actual["type"]);
    },
  })
  .reply(204);

.passThrough() forwards the matched request over network

// Mock POST requests to /api with HTTP 201, but forward
// GET requests to server
mock
  .onPost(/^\/api/)
  .reply(201)
  .onGet(/^\/api/)
  .passThrough();

Recall that the order of handlers is significant

// Mock specific requests, but let unmatched ones through
mock
  .onGet("/foo")
  .reply(200)
  .onPut("/bar", { xyz: "abc" })
  .reply(204)
  .onAny()
  .passThrough();

Note that passThrough requests are not subject to delaying by delayResponse.

If you set onNoMatch option to passthrough all requests would be forwarded over network by default

// Mock all requests to /foo with HTTP 200, but forward
// any others requests to server
var mock = new MockAdapter(axiosInstance, { onNoMatch: "passthrough" });

mock.onAny("/foo").reply(200);

Using onNoMatch option with throwException to throw an exception when a request is made without match any handler. It's helpful to debug your test mocks.

var mock = new MockAdapter(axiosInstance, { onNoMatch: "throwException" });

mock.onAny("/foo").reply(200);

axios.get("/unexistent-path");

// Exception message on console:
//
// Could not find mock for: 
// {
//   "method": "get",
//   "url": "http://localhost/unexistent-path"
// }

As of 1.7.0, reply function may return a Promise:

mock.onGet("/product").reply(function (config) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      if (Math.random() > 0.1) {
        resolve([200, { id: 4, name: "foo" }]);
      } else {
        // reject() reason will be passed as-is.
        // Use HTTP error status code to simulate server failure.
        resolve([500, { success: false }]);
      }
    }, 1000);
  });
});

Composing from multiple sources with Promises:

var normalAxios = axios.create();
var mockAxios = axios.create();
var mock = new MockAdapter(mockAxios);

mock
  .onGet("/orders")
  .reply(() =>
    Promise.all([
      normalAxios.get("/api/v1/orders").then((resp) => resp.data),
      normalAxios.get("/api/v2/orders").then((resp) => resp.data),
      { id: "-1", content: "extra row 1" },
      { id: "-2", content: "extra row 2" },
    ]).then((sources) => [
      200,
      sources.reduce((agg, source) => agg.concat(source)),
    ])
  );

History

The history property allows you to enumerate existing axios request objects. The property is an object of verb keys referencing arrays of request objects.

This is useful for testing.

describe("Feature", () => {
  it("requests an endpoint", (done) => {
    var mock = new AxiosMockAdapter(axios);
    mock.onPost("/endpoint").replyOnce(200);

    feature
      .request()
      .then(() => {
        expect(mock.history.post.length).toBe(1);
        expect(mock.history.post[0].data).toBe(JSON.stringify({ foo: "bar" }));
      })
      .then(done)
      .catch(done.fail);
  });
});

You can clear the history with resetHistory

mock.resetHistory();

Release Notes

1.21.1
By Colin Timmermans • Published on June 1, 2022
  • Correctly include UMD builds in npm package
1.21.0
By Colin Timmermans • Published on June 1, 2022
  • Added support for LINK and UNLINK HTTP methods (a22ffe54d87c9081c14e9135c34dc63410b5693b)
  • Handle params and payloads on delete requests (d0e70817c9cc816efe440ff749c4bc22275ce667)
  • Vendor isBlob package to get rid of arrow function (e1700fdcb7e483a21cc1654c1031fe591db27eac)
1.20.0
By Colin Timmermans • Published on August 13, 2021
  • Expose originalAdapter in TypeScript types (e6b2d2f6315d4caae0a39992e696b28454811591)
  • Adopt the official behavior of validateStatus and always return an error instance on rejection (43e35b68cd5abdb7a39403a6f741036123f907f8)
  • Ensure that an instance is provided to mock (a326853900967b2f32fc0a873cc5a23d221d8a36)
  • Allow Blob responses (8dd3039ff12da83c153c816188a68aa89df44df9)
1.19.0
By Colin Timmermans • Published on October 25, 2020
  • Add toJSON method to axios errors (a14b283)
  • Create onNoMatch=throwException option (a52b450)
  • Support asymmetricMatch in TypeScript (1a22ea2)
  • Handle request with undefined url (78fe012)
  • Add onNoMatch: "throwException" to types (855c8a5)
  • fix responseURL case (95d2aeb)
1.18.2
By Colin Timmermans • Published on July 18, 2020
  • fix for IE11 (1691b9d)
1.18.1
By Colin Timmermans • Published on March 22, 2020
  • Don't run bundle size test in prepublish, which prevented unminified umd bundle to be included in the npm package (e047ae2)
1.18.0
By Colin Timmermans • Published on March 22, 2020
  • Support streams and buffers as a mocked response (b69fc6f)
  • Add request object to response (0ba1ad3)
  • Set isAxiosError property on errors (239ddda)
  • Make response type generic (b493d9c)
  • Add option to pass through unmatched requests to axios (914979f)
  • Add support for asymmetric matchers (0561369)
  • Add Options handler (51eea7c)
  • Add abortRequest and abortRequestOnce (8ccdef2)
  • Use timeoutErrorMessage if provided (262c224)
1.17.0
By Colin Timmermans • Published on July 2, 2019
  • Add resetHandlers and resetHistory types (456d085)
  • Declare resetHistory method in MockAdapter class (9064f2e)
  • Fix timeout, timeoutOnce, networkError, networkErrorOnce TS typedefs (7bd34fb)
  • Make sure interceptors are only called once when using passThrough (fdb32dd)
  • Correctly handle baseURL when using passthrough (fdb32dd)
1.16.0
By Colin Timmermans • Published on January 2, 2019
  • Use a local variable instead of modifying config.url (8abf4a5)
  • Add networkErrorOnce and timeoutOnce (4a0e874)
  • Check for functions with typeof (0901797)
  • Implement redirect mocking (447c64b)
  • Add list as an http verb (3aaa145)
  • Improved TypeScript type definitions (592b400, 027a821, 677f1f1)
1.15.0
By Colin Timmermans • Published on April 5, 2018
  • support for mocks when an interceptor retries a request (#119) (dd8b660)
  • Implemented history to record all calls to the mock (#124) (2eb55cc)

General

License
MIT
Typescript Types
Built-in
Tree-shakeable
No

Popularity

GitHub Stargazers
3,025
Community Interest
3,007
Number of Forks
212

Maintenance

Commits
10/219/2208
Last Commit
Open Issues
71
Closed Issues
143
Open Pull Requests
13
Closed Pull Requests
38

Versions

Versions Released
10/219/2202
Latest Version Released
Jun 1, 2022
Current Tags
latest1.21.1

Contributors

ctimmerm
ctimmerm
Commits: 117
danii-nebot
danii-nebot
Commits: 3
StarpTech
StarpTech
Commits: 3
cross311
cross311
Commits: 3
koenpunt
koenpunt
Commits: 3
gilbsgilbs
gilbsgilbs
Commits: 3
davidlewallen
davidlewallen
Commits: 2
hlissnake
hlissnake
Commits: 2
sakumikko
sakumikko
Commits: 2
joebowbeer
joebowbeer
Commits: 2
jDeppen
jDeppen
Commits: 1
asztal
asztal
Commits: 1
zachrip
zachrip
Commits: 1
thefat32
thefat32
Commits: 1
dingziqi
dingziqi
Commits: 1