Registry / testing / axios-mock-adapter

axios-mock-adapter

JSON →
library2.1.0jsnpmunverified

axios-mock-adapter is a lightweight mocking library for Axios that allows you to easily mock HTTP requests in tests by intercepting Axios calls and returning custom responses. v2.1.0 supports Axios >= 0.17.0 and works in both Node.js and browser environments. It provides a simple API for mocking requests by method, URL, parameters, and regex, with support for replyOnce, delays, network errors, and timeouts. Unlike nock or msw, it directly replaces the Axios adapter without requiring server setup, making it ideal for unit tests.

npm install axios-mock-adapter
INSTALL
IMPORT
SIG · AXIOS-MOCK-ADAPTER
A
axios-mock-adapter
testingjavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

AxiosMockAdapter
import AxiosMockAdapter from 'axios-mock-adapter';
const AxiosMockAdapter = require('axios-mock-adapter').default;
Default export; in CommonJS, use require('axios-mock-adapter') directly, not .default.
MockAdapterOptions
import type { MockAdapterOptions } from 'axios-mock-adapter';
import { MockAdapterOptions } from 'axios-mock-adapter';
TypeScript: use type import for the options interface.
AxiosMockAdapter (constructor)
const mock = new AxiosMockAdapter(axiosInstance);
const mock = new AxiosMockAdapter();
The constructor requires an Axios instance or default axios as the first argument.

Shows how to mock a GET request with axios-mock-adapter, including import, mock setup, and response handling.

import axios from 'axios'; import AxiosMockAdapter from 'axios-mock-adapter'; const mock = new AxiosMockAdapter(axios); mock.onGet('/users').reply(200, { users: [{ id: 1, name: 'John' }] }); axios.get('/users').then(response => { console.log(response.data); mock.restore(); });
Debug
Known issues
breakingAxios >= 0.17.0 required; adapter API changed significantly in Axios 0.17.0
fix
Upgrade axios to >= 0.17.0
affects: <2.0.0
breakingv2.0.0 changed from CommonJS to ESM-first; require() may not work as expected in some environments
fix
Use import syntax or enable CommonJS interop in bundler
affects: >=2.0.0
deprecatedonGet, onPost etc. with .once() chaining deprecated in favor of .replyOnce()
fix
Use .replyOnce() directly
affects: >=2.0.0
gotchaMock must be restored after each test to avoid state leakage between tests
fix
Call mock.restore() in afterEach or teardown
affects: >=0.0.0
gotchaParams matching is strict; all key/value pairs must match exactly; omitting a param fails
fix
Ensure params object includes all expected keys
affects: >=0.0.0
Errors
Common errors & fixes
Error: AxiosMockAdapter is not a constructor
Using default import incorrectly in CommonJS or wrong import path
fix
Use const AxiosMockAdapter = require('axios-mock-adapter').default; or use import syntax
TypeError: Cannot read properties of undefined (reading 'request')
Mock adapter not attached to an axios instance; constructor called without valid instance
fix
Pass an existing axios instance or import axios as const mock = new AxiosMockAdapter(axios);
Error: Network Error
mock.onGet('/path').networkError() triggered explicit network error
fix
Use .reply() instead of .networkError() if you intend to return a success response
AxiosError: timeout of 0ms exceeded
Mock timeout set via mock.onGet('/path').timeout() or delayResponse too low
fix
Increase delayResponse or remove timeout mock
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
axiosrequiredpeer dependency; the mock adapter replaces Axios's default adapter
Agent activity
23 hits · last 30 days
node
20
OpenAI (training)
1
Resources
axios-mock-adapter — npm install axios-mock-adapter · libregistry