axios-mock-adapter vs fetch-mock
フロントエンドテストにおけるHTTPモックライブラリの選択
axios-mock-adapterfetch-mock類似パッケージ:
フロントエンドテストにおけるHTTPモックライブラリの選択

axios-mock-adapterfetch-mock は、それぞれ異なるHTTPクライアント(Axios と Fetch API)向けに設計されたモックライブラリです。両方ともユニットテストや統合テストにおいてネットワークリクエストをインターセプトし、擬似的なレスポンスを返すことで、安定したテスト環境を提供します。axios-mock-adapter は Axios インスタンスに直接アダプターとして接続され、fetch-mock はグローバルな fetch 関数を置き換える形で動作します。どちらも開発者が外部APIに依存せずにロジックを検証できるように支援します。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
axios-mock-adapter1,950,4533,54567.9 kB931年前MIT
fetch-mock976,9691,310157 kB21ヶ月前MIT

axios-mock-adapter vs fetch-mock: テストにおけるHTTPモック戦略の比較

フロントエンド開発では、外部APIへの依存を排除して信頼性の高いテストを書くために、HTTPリクエストをモックする必要があります。axios-mock-adapterfetch-mock はそれぞれ異なるHTTPクライアント向けに特化したモックツールですが、その設計思想と使い勝手には明確な違いがあります。実際のテストシナリオに基づいて、両者の技術的特性を比較します。

🧪 基本的なセットアップと使用方法

axios-mock-adapter は Axios インスタンスに直接アタッチされます。モックはそのインスタンスに対してのみ有効で、他の Axios インスタンスや fetch には影響しません。

// axios-mock-adapter の基本的な使い方
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

const mock = new MockAdapter(axios);
mock.onGet('/users').reply(200, [{ id: 1, name: '太郎' }]);

// 実際の呼び出し
const response = await axios.get('/users');
console.log(response.data); // [{ id: 1, name: '太郎' }]

fetch-mock はグローバルな fetch 関数を上書きします。そのため、どのモジュールから fetch が呼ばれたとしても、マッチするルートがあればモックが適用されます。

// fetch-mock の基本的な使い方
import fetchMock from 'fetch-mock';

fetchMock.get('/users', [{ id: 1, name: '太郎' }]);

// 実際の呼び出し
const response = await fetch('/users');
const data = await response.json();
console.log(data); // [{ id: 1, name: '太郎' }]

🔁 モックの有効範囲と隔離性

axios-mock-adapter はインスタンス単位で動作するため、テスト間の干渉が起きにくいのが強みです。例えば、複数のテストファイルで異なる Axios インスタンスを使えば、モック設定が混ざることはありません。

// 複数の Axios インスタンスで独立したモック
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

const api1 = axios.create();
const api2 = axios.create();

const mock1 = new MockAdapter(api1);
const mock2 = new MockAdapter(api2);

mock1.onGet('/data').reply(200, { source: 'api1' });
mock2.onGet('/data').reply(200, { source: 'api2' });

// 各インスタンスは独立して動作
const res1 = await api1.get('/data'); // { source: 'api1' }
const res2 = await api2.get('/data'); // { source: 'api2' }

一方、fetch-mock はグローバル状態を操作するため、テストの前後にクリーンアップ処理を忘れると、他のテストに影響を与える可能性があります。

// fetch-mock でのテスト前後のクリーンアップ
import fetchMock from 'fetch-mock';

beforeEach(() => {
  fetchMock.reset();
});

afterEach(() => {
  fetchMock.restore(); // グローバル fetch を元に戻す
});

it('ユーザー一覧を取得', async () => {
  fetchMock.get('/users', [{ id: 1 }]);
  const res = await fetch('/users');
  // ...
});

🎯 URLマッチングと動的パラメータ

両方とも柔軟なURLマッチングをサポートしていますが、記述方法が異なります。

axios-mock-adapter では、文字列、正規表現、またはワイルドカードを使ったパス指定が可能です。

// axios-mock-adapter での動的パス
mock.onGet('/users/123').reply(200, { id: 123 });
mock.onGet(/\/users\/\d+/).reply(200, { id: 'dynamic' });
mock.onGet('/users/:id').reply((config) => {
  const id = config.url.split('/')[2];
  return [200, { id }];
});

fetch-mock は、文字列、正規表現、またはパスパターン(:id 形式)をサポートします。また、マッチしたパラメータをコールバックで受け取れます。

// fetch-mock での動的パス
fetchMock.get('/users/123', { id: 123 });
fetchMock.get(/\/users\/\d+/, { id: 'dynamic' });
fetchMock.get('/users/:id', (url, opts, params) => {
  return { id: params.id };
});

🔄 モックの解除と再利用

axios-mock-adapter は、mock.restore() を呼ぶことで特定のインスタンスのモックを無効にできます。また、mock.reset() で登録されたハンドラをクリアできます。

// axios-mock-adapter のリセット
mock.reset(); // ハンドラをクリア
mock.restore(); // インターセプトを完全に解除

fetch-mock は、reset() でルート定義をクリアし、restore() でグローバル fetch を元の状態に戻します。

// fetch-mock のリセット
fetchMock.reset(); // ルート定義をクリア
fetchMock.restore(); // グローバル fetch を復元

🧩 統合テストとの相性

axios-mock-adapter は、Axios をラップしたカスタムAPIクライアントを使っているプロジェクトで特に便利です。そのクライアント内部の Axios インスタンスにモックを差し込めば、アプリケーションコードを一切変更せずにテストできます。

// カスタムAPIクライアントの例
const apiClient = axios.create({ baseURL: '/api' });

// テストではこのインスタンスにモックを適用
const mock = new MockAdapter(apiClient);

fetch-mock は、React Query や SWR など、内部で fetch を使うライブラリと組み合わせる際に自然に使えます。ただし、これらのライブラリが独自のキャッシング機構を持っている場合、モックだけでは十分でないこともあります。

// React Query + fetch-mock
fetchMock.get('/api/data', { data: 'mocked' });

function useData() {
  return useQuery('data', () => fetch('/api/data').then(r => r.json()));
}

⚠️ 注意点と落とし穴

  • axios-mock-adapter は Axios 専用なので、プロジェクトで fetch を使っている場合は使えません。また、Axios の非同期処理の内部構造に依存しているため、Axios のメジャーアップデートで破壊的変更が入る可能性があります。

  • fetch-mock はグローバル状態を変更するため、並列テスト実行時や、モックを忘れたまま他のテストが走る場合に予期しない挙動を引き起こすことがあります。必ず beforeEach / afterEach でクリーンアップすることを推奨します。

📌 まとめ:どちらを選ぶべきか?

観点axios-mock-adapterfetch-mock
対応クライアントAxios 専用ネイティブ fetch
スコープインスタンス単位(局所的)グローバル(広範囲)
テスト隔離性高い(インスタンス分離)中(クリーンアップ必須)
導入の容易さAxios 使用時に自然どのプロジェクトでも可能
動的パラメータ可能(コールバックで解析)可能(params で取得)

Axios を使っているなら axios-mock-adapterfetch を使っているなら fetch-mock を選ぶのが最も安全でシンプルです。 プロジェクト全体でHTTPクライアントを統一している場合、対応するモックライブラリを選ぶことで、テストコードのメンテナンス性と信頼性を高められます。

選び方: axios-mock-adapter vs fetch-mock
  • axios-mock-adapter:

    axios-mock-adapter は、プロジェクトで Axios を使用している場合に最適です。Axios のインスタンス単位でモックを設定でき、複数のインスタンスを個別に制御したいケースや、既存の Axios ベースのコードベースに最小限の変更でモック機能を追加したい場合に適しています。ただし、Fetch API を使っているプロジェクトでは使えません。

  • fetch-mock:

    fetch-mock は、ネイティブの fetch API を使っているアプリケーションや、軽量なモック機構を求める場合に適しています。グローバルな fetch を置き換えるため、フレームワークやライブラリに依存せず広く利用できます。ただし、モックの有効化・無効化には明示的なクリーンアップが必要で、テスト間の干渉を避けるために注意が必要です。

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();