axios-mock-adapter vs fetch-mock vs mockttp vs nock
HTTP请求模拟库
axios-mock-adapterfetch-mockmockttpnock类似的npm包:

HTTP请求模拟库

HTTP请求模拟库用于在开发和测试阶段模拟HTTP请求和响应。这些库可以帮助开发者在不依赖真实后端服务的情况下进行前端开发和单元测试,从而提高开发效率和测试的可靠性。通过模拟网络请求,开发者可以快速验证应用程序的行为,确保在与后端交互时的逻辑正确性。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
axios-mock-adapter2,055,6793,55167.9 kB961 年前MIT
fetch-mock1,176,6601,309157 kB64 个月前MIT
mockttp336,9928501.84 MB404 个月前Apache-2.0
nock013,081185 kB8317 天前MIT

功能对比: axios-mock-adapter vs fetch-mock vs mockttp vs nock

集成与兼容性

  • axios-mock-adapter:

    axios-mock-adapter专为axios设计,能够无缝集成到现有的axios请求中,使用非常简单,适合已经在使用axios的项目。

  • fetch-mock:

    fetch-mock与原生fetch API兼容,能够轻松地在使用fetch的项目中进行请求模拟,支持多种请求类型和响应格式。

  • mockttp:

    mockttp是一个独立的HTTP服务器模拟库,能够与任何HTTP客户端配合使用,适合需要更高自定义的测试场景。

  • nock:

    nock专为Node.js设计,能够拦截HTTP请求并提供模拟响应,适合Node.js环境中的测试。

功能与灵活性

  • axios-mock-adapter:

    axios-mock-adapter提供了简单的API来设置和清理模拟请求,支持多种响应类型和延迟设置,灵活性较高。

  • fetch-mock:

    fetch-mock功能强大,支持多种模拟场景,包括延迟、错误响应和条件响应,适合复杂的请求模拟需求。

  • mockttp:

    mockttp支持HTTP和HTTPS请求的模拟,能够模拟网络延迟、错误和其他条件,灵活性极高,适合需要复杂网络场景的测试。

  • nock:

    nock能够拦截和模拟HTTP请求,支持多种配置选项,适合复杂的请求场景,灵活性较高。

学习曲线

  • axios-mock-adapter:

    axios-mock-adapter的学习曲线较平缓,特别适合已经熟悉axios的开发者,使用起来非常直观。

  • fetch-mock:

    fetch-mock的学习曲线相对较低,文档清晰,易于上手,适合新手和有经验的开发者。

  • mockttp:

    mockttp的学习曲线稍陡,需要对HTTP协议有一定了解,但提供了丰富的功能,适合需要深度定制的开发者。

  • nock:

    nock的学习曲线适中,虽然功能强大,但需要一定的Node.js基础,适合有经验的开发者。

性能与效率

  • axios-mock-adapter:

    axios-mock-adapter在模拟请求时性能表现良好,能够快速响应模拟请求,适合高频率的请求测试场景。

  • fetch-mock:

    fetch-mock在处理复杂请求时性能稳定,能够有效模拟多种响应场景,适合需要高效测试的项目。

  • mockttp:

    mockttp在模拟网络条件时性能表现优异,能够处理大量并发请求,适合高负载测试场景。

  • nock:

    nock在Node.js环境中性能表现良好,能够高效拦截和模拟请求,适合后端服务的单元测试。

维护与社区支持

  • axios-mock-adapter:

    axios-mock-adapter有良好的社区支持,文档齐全,易于维护,适合长期项目使用。

  • fetch-mock:

    fetch-mock活跃的社区和良好的文档支持使其易于维护,适合快速迭代的项目。

  • mockttp:

    mockttp的文档详细,社区活跃,能够提供良好的维护支持,适合需要长期使用的项目。

  • nock:

    nock有着强大的社区支持和丰富的文档,适合长期维护的项目。

如何选择: axios-mock-adapter vs fetch-mock vs mockttp vs nock

  • axios-mock-adapter:

    选择axios-mock-adapter如果你已经在项目中使用axios进行HTTP请求,并希望在测试中轻松模拟axios的请求和响应。它与axios紧密集成,使用简单,能够快速设置和清理模拟。

  • fetch-mock:

    选择fetch-mock如果你的项目使用原生的fetch API进行HTTP请求。fetch-mock提供了灵活的模拟功能,支持多种请求类型和响应格式,适合需要模拟复杂请求场景的项目。

  • mockttp:

    选择mockttp如果你需要一个强大的HTTP服务器模拟工具,支持HTTP和HTTPS请求,并且能够模拟延迟、错误和其他网络条件。它适合需要更高自定义和控制的测试场景。

  • nock:

    选择nock如果你需要在Node.js环境中进行HTTP请求的模拟。nock能够拦截HTTP请求并提供模拟响应,适合后端服务的单元测试和集成测试。它支持多种配置选项,能够处理复杂的请求场景。

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

const axios = require("axios");
const AxiosMockAdapter = require("axios-mock-adapter");

// This sets the mock adapter on the default instance
const mock = new AxiosMockAdapter(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

const axios = require("axios");
const AxiosMockAdapter = require("axios-mock-adapter");

// This sets the mock adapter on the default instance
const mock = new AxiosMockAdapter(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:
const mock = new AxiosMockAdapter(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 },
    {
      headers: 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
const mock = new AxiosMockAdapter(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.

const mock = new AxiosMockAdapter(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:

const normalAxios = axios.create();
const mockAxios = axios.create();
const mock = new AxiosMockAdapter(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) => {
    const 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();