axios-mock-adapter and fetch-mock are both widely used libraries for intercepting and mocking HTTP requests during frontend testing, but they target different underlying HTTP clients. axios-mock-adapter is specifically designed to mock requests made with the axios HTTP client by hooking into its request interceptor system. In contrast, fetch-mock mocks the native window.fetch API (or Node.js equivalents), making it suitable for applications that rely directly on fetch without an abstraction layer. Both enable deterministic test environments by simulating network responses without hitting real endpoints.
When writing frontend tests, you often need to simulate network responses without making real HTTP calls. Both axios-mock-adapter and fetch-mock solve this problem — but they operate at different layers of the networking stack. Choosing the right tool depends entirely on which HTTP client your app actually uses.
axios-mock-adapter only mocks requests made through axios. It works by attaching to axios’s internal interceptor pipeline, so it never touches the actual network layer.
// axios-mock-adapter only affects axios calls
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
const mock = new MockAdapter(axios);
mock.onGet('/users').reply(200, [{ id: 1, name: 'Alice' }]);
// This is mocked
const res = await axios.get('/users');
// But this bypasses the mock entirely
const realRes = await fetch('/users'); // hits the real network!
fetch-mock mocks the global fetch function itself. It replaces window.fetch (or global.fetch in Node) so any code using fetch — including third-party libraries — will be intercepted.
// fetch-mock affects all fetch calls
import fetchMock from 'fetch-mock';
fetchMock.get('/users', [{ id: 1, name: 'Alice' }]);
// This is mocked
const res = await fetch('/users');
// So is this (if some-lib uses fetch internally)
const libRes = await someLibraryThatUsesFetch();
// But axios calls are unaffected
const axiosRes = await axios.get('/users'); // real network call
💡 Key Insight: These tools are not interchangeable. Use
axios-mock-adapteronly with axios. Usefetch-mockonly withfetch. Mixing them leads to unmocked requests and flaky tests.
Both libraries support flexible route matching, but their syntax differs based on their target APIs.
axios-mock-adapter matches based on method + URL, and supports regex, wildcards, and custom matchers:
const mock = new MockAdapter(axios);
// Exact match
mock.onGet('/users').reply(200, users);
// Regex match
mock.onGet(/\/users\/\d+/).reply(200, user);
// With query params
mock.onGet('/search', { q: 'react' }).reply(200, results);
// Custom matcher function
mock.onGet((config) => config.url.includes('admin')).reply(403);
fetch-mock uses a matcher-first approach where the first argument defines what to intercept:
// String path
fetchMock.get('/users', users);
// Regex
fetchMock.get(/\/users\/\d+/, user);
// Path with query
fetchMock.get('/search?q=react', results);
// Custom matcher function
fetchMock.get((url, opts) => url.includes('admin'), 403);
// Also supports POST body matching
fetchMock.post('/login', (url, opts) => {
return opts.body.includes('valid-token');
}, { status: 200 });
Both handle dynamic segments well, but fetch-mock’s matcher functions receive more context (like request options), which can be useful for complex assertions.
Clean test isolation prevents state leakage between test cases — a common source of flakiness.
axios-mock-adapter requires manual cleanup:
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore(); // removes interceptors
});
// Or reset history
mock.reset(); // clears recorded calls but keeps routes
fetch-mock provides built-in sandboxing and automatic cleanup:
// Option 1: Global instance (requires manual reset)
afterEach(() => fetchMock.reset());
// Option 2: Create isolated sandboxes per test
const fetchMockSandbox = fetchMock.sandbox();
fetchMockSandbox.get('/users', users);
// This sandbox doesn’t affect other tests
await fetchMockSandbox('/users');
The sandbox feature in fetch-mock is especially valuable in large test suites where concurrent or overlapping mocks could interfere.
Both libraries let you inspect and assert on request details.
axios-mock-adapter exposes request data via the config object in custom handlers:
mock.onPost('/login').reply((config) => {
const { username, password } = JSON.parse(config.data);
if (username === 'admin') {
return [200, { token: 'fake-jwt' }];
}
return [401];
});
// Later, assert what was sent
expect(mock.history.post[0].data).toBe(JSON.stringify({ username: 'admin' }));
fetch-mock gives access to the full Request object in matchers and allows inspection via .calls():
fetchMock.post('/login', (url, opts) => {
const body = JSON.parse(opts.body);
return body.username === 'admin' ? { token: 'fake-jwt' } : 401;
});
// Assert on actual calls
const calls = fetchMock.calls('/login');
expect(JSON.parse(calls[0][1].body)).toEqual({ username: 'admin' });
Both approaches work well, but fetch-mock’s use of standard fetch options aligns more closely with browser APIs.
axios-mock-adapter works wherever axios runs — browser or Node.js — because it operates at the axios layer, not the network layer.
fetch-mock requires a fetch implementation. In Node.js, you must provide one (e.g., node-fetch, undici, or cross-fetch). The library itself doesn’t include a polyfill.
// In Node.js test setup
global.fetch = require('node-fetch');
// Now fetch-mock works
This isn’t a limitation per se, but it’s a setup step you must handle explicitly when testing non-browser environments.
You’re building a dashboard with axios for all API calls.
axios-mock-adapter// test/utils/mockApi.js
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
export const mockApi = () => {
const mock = new MockAdapter(axios);
mock.onGet('/profile').reply(200, { name: 'Dev' });
return mock;
};
Your framework encourages direct fetch usage (e.g., Remix loaders).
fetch-mock// app/routes/dashboard.test.jsx
import fetchMock from 'fetch-mock';
beforeEach(() => {
fetchMock.get('/api/user', { name: 'Tester' });
});
afterEach(() => fetchMock.reset());
Your codebase uses both axios and fetch.
| Feature | axios-mock-adapter | fetch-mock |
|---|---|---|
| Mocks | axios requests only | native fetch calls |
| Setup Complexity | Low (just pass axios instance) | Medium (may need fetch polyfill in Node) |
| Route Matching | Method + URL + custom functions | Flexible matchers (string, regex, fn) |
| Test Isolation | Manual (restore(), reset()) | Built-in sandboxes + reset() |
| Request Inspection | Via mock.history | Via fetchMock.calls() |
| Works in Node.js | Yes (via axios) | Yes (with fetch polyfill) |
| Third-Party Lib Support | Only if they use your axios instance | Yes (if they use fetch) |
Don’t choose based on popularity — choose based on your actual HTTP client:
axios-mock-adapter. It’s purpose-built, lightweight, and integrates cleanly.fetch, use fetch-mock. It’s the standard way to mock the native API reliably.Trying to force one to mock the other’s domain leads to gaps in coverage and confusing test failures. Keep your mocking layer aligned with your networking layer — your future self (and teammates) will thank you.
Choose axios-mock-adapter if your application uses axios as its primary HTTP client and you want a tightly integrated mocking solution that leverages axios’s interceptor architecture. It provides a clean, chainable API that mirrors axios’s own patterns and handles edge cases like request cancellation and custom instance configuration naturally. Avoid it if your codebase doesn’t use axios — it won’t intercept fetch calls.
Choose fetch-mock if your project uses the native fetch API (with or without lightweight wrappers) or runs in environments where fetch is the standard networking primitive (e.g., modern browsers, service workers, or frameworks like Remix). It offers fine-grained control over route matching, supports sandboxed mocking for isolated tests, and works consistently across browser and Node.js environments via compatible fetch implementations. Don’t use it to mock axios requests unless you’ve polyfilled or replaced axios with fetch under the hood.
Axios adapter that allows to easily mock requests
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.
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)),
])
);
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();