Core functionality for creating, configuring, and managing axios mock adapters. The AxiosMockAdapter class is the primary interface for intercepting axios requests and providing mock responses.
The AxiosMockAdapter class includes a static default property that references the class itself, useful for default exports.
/**
* Static reference to the AxiosMockAdapter class
* Used for supporting both named and default imports
*/
static default: typeof AxiosMockAdapter;Usage Example:
// Both of these work identically
const AxiosMockAdapter = require("axios-mock-adapter");
const AxiosMockAdapter = require("axios-mock-adapter").default;Creates a new mock adapter instance that intercepts requests on the provided axios instance.
/**
* Creates a new axios mock adapter
* @param axiosInstance - Axios instance to mock (required)
* @param options - Optional configuration object
*/
constructor(axiosInstance, options);
interface MockAdapterOptions {
/** Delay in milliseconds for all responses */
delayResponse?: number;
/** Behavior when no mock handler matches a request */
onNoMatch?: 'passthrough' | 'throwException';
}Usage Examples:
const axios = require("axios");
const AxiosMockAdapter = require("axios-mock-adapter");
// Basic setup
const mock = new AxiosMockAdapter(axios);
// With global delay
const mockWithDelay = new AxiosMockAdapter(axios, { delayResponse: 1000 });
// Pass through unmatched requests to real network
const mockPassthrough = new AxiosMockAdapter(axios, { onNoMatch: "passthrough" });
// Throw exceptions for unmatched requests
const mockStrict = new AxiosMockAdapter(axios, { onNoMatch: "throwException" });Returns the adapter function that axios uses internally. Rarely used directly.
/**
* Returns the adapter function for axios
* @returns Function that handles axios requests
*/
adapter();Restores the original axios adapter, completely removing mock behavior.
/**
* Restores original axios adapter and removes mocking completely
* After calling restore(), the axios instance behaves normally
*/
restore();Usage Example:
const mock = new AxiosMockAdapter(axios);
mock.onGet("/test").reply(200);
// Later, completely remove mocking
mock.restore();
// Now axios.get("/test") will make real HTTP requestResets both mock handlers and request history, but keeps mocking active.
/**
* Resets both mock handlers and request history
* Equivalent to calling resetHandlers() and resetHistory()
*/
reset();Clears all registered mock handlers but keeps mocking active and preserves history.
/**
* Clears all registered mock handlers
* Mocking remains active but no handlers are registered
*/
resetHandlers();Usage Example:
const mock = new AxiosMockAdapter(axios);
mock.onGet("/users").reply(200, []);
mock.onPost("/users").reply(201);
// Clear all handlers but keep mocking active
mock.resetHandlers();
// Now all requests will return 404 (no matching handlers)Clears the request history but keeps mock handlers and mocking active.
/**
* Clears the request history array
* Mock handlers remain active
*/
resetHistory();Usage Example:
// Make some requests
axios.get("/test");
axios.post("/test");
console.log(mock.history.get.length); // 1
console.log(mock.history.post.length); // 1
// Clear history
mock.resetHistory();
console.log(mock.history.get.length); // 0
console.log(mock.history.post.length); // 0Array that tracks all intercepted requests, with per-method arrays for easy filtering.
/**
* Array of all intercepted requests with per-method properties
*/
history: HistoryArray;
interface HistoryArray extends Array {
/** Array of GET requests */
get: Array;
/** Array of POST requests */
post: Array;
/** Array of PUT requests */
put: Array;
/** Array of DELETE requests */
delete: Array;
/** Array of PATCH requests */
patch: Array;
/** Array of HEAD requests */
head: Array;
/** Array of OPTIONS requests */
options: Array;
/** Array of LIST requests */
list: Array;
/** Array of LINK requests */
link: Array;
/** Array of UNLINK requests */
unlink: Array;
}Usage Example:
const mock = new AxiosMockAdapter(axios);
mock.onAny().reply(200);
// Make requests
await axios.get("/users");
await axios.post("/users", { name: "John" });
// Check history
console.log(mock.history.get.length); // 1
console.log(mock.history.post.length); // 1
console.log(mock.history.get[0].url); // "/users"
console.log(mock.history.post[0].data); // '{"name":"John"}'
// Total requests
console.log(mock.history.length); // 2The constructor throws an error if no axios instance is provided:
try {
const mock = new AxiosMockAdapter();
} catch (error) {
console.log(error.message); // "Please provide an instance of axios to mock"
}When onNoMatch is set to 'throwException', unmatched requests throw detailed errors:
const mock = new AxiosMockAdapter(axios, { onNoMatch: "throwException" });
try {
await axios.get("/unmatched");
} catch (error) {
console.log(error.isCouldNotFindMockError); // true
console.log(error.method); // "get"
console.log(error.url); // "/unmatched"
}