Axios adapter that allows to easily mock requests
npx @tessl/cli install tessl/npm-axios-mock-adapter@2.1.0Axios Mock Adapter is a comprehensive axios request mocking library that enables developers to easily intercept and mock HTTP requests in testing environments. It offers a chainable API for setting up mock responses based on request methods, URLs, parameters, and request data, with support for regex patterns, asymmetric matchers, and dynamic response functions.
npm install axios-mock-adapter --save-devconst AxiosMockAdapter = require("axios-mock-adapter");
// Alternative default import (also available)
const AxiosMockAdapter = require("axios-mock-adapter").default;For ES modules:
import AxiosMockAdapter from "axios-mock-adapter";const axios = require("axios");
const AxiosMockAdapter = require("axios-mock-adapter");
// Create mock adapter
const mock = new AxiosMockAdapter(axios);
// Mock GET request and set response
mock.onGet("/users").reply(200, {
users: [{ id: 1, name: "John Smith" }]
});
// Make request (will be intercepted)
axios.get("/users").then(function (response) {
console.log(response.data); // { users: [{ id: 1, name: "John Smith" }] }
});
// Clean up
mock.restore();Axios Mock Adapter is built around several key components:
Core functionality for creating, configuring, and managing axios mock adapters.
class AxiosMockAdapter {
static default: typeof AxiosMockAdapter;
constructor(axiosInstance, options);
adapter();
restore();
reset();
resetHandlers();
resetHistory();
history: HistoryArray;
}
interface MockAdapterOptions {
delayResponse?: number;
onNoMatch?: 'passthrough' | 'throwException';
}Comprehensive HTTP method mocking with support for all standard HTTP verbs and flexible parameter matching.
// Methods without request body
onGet(matcher?, config?): RequestHandler;
onDelete(matcher?, config?): RequestHandler;
onHead(matcher?, config?): RequestHandler;
onOptions(matcher?, config?): RequestHandler;
// Methods with request body
onPost(matcher?, body?, config?): RequestHandler;
onPut(matcher?, body?, config?): RequestHandler;
onPatch(matcher?, body?, config?): RequestHandler;
onList(matcher?, body?, config?): RequestHandler;
onLink(matcher?, body?, config?): RequestHandler;
onUnlink(matcher?, body?, config?): RequestHandler;
// Universal method
onAny(matcher?, config?): RequestHandler;Flexible response configuration with support for static responses, dynamic functions, and various response formats.
interface RequestHandler {
reply(statusOrCallback, data?, headers?): AxiosMockAdapter;
replyOnce(statusOrCallback, data?, headers?): AxiosMockAdapter;
withDelayInMs(delay: number): RequestHandler;
}
type CallbackResponseSpecFunc = (config) => MockResponse | Promise<MockResponse>;
type MockResponse = [number, any?, object?] | { status: number; data: any; headers?: object };Advanced error simulation capabilities for testing error handling and network failure scenarios.
interface RequestHandler {
passThrough(): AxiosMockAdapter;
networkError(): AxiosMockAdapter;
networkErrorOnce(): AxiosMockAdapter;
timeout(): AxiosMockAdapter;
timeoutOnce(): AxiosMockAdapter;
abortRequest(): AxiosMockAdapter;
abortRequestOnce(): AxiosMockAdapter;
}interface HistoryArray extends Array {
get: Array;
post: Array;
put: Array;
delete: Array;
patch: Array;
head: Array;
options: Array;
list: Array;
link: Array;
unlink: Array;
}
interface AsymmetricMatcher {
asymmetricMatch: Function;
}
interface ConfigMatcher {
params?: object | AsymmetricMatcher;
headers?: object | AsymmetricMatcher;
data?: any | AsymmetricMatcher;
}
interface CouldNotFindMockError extends Error {
isCouldNotFindMockError: boolean;
url: string;
method: string;
}
type UrlMatcher = string | RegExp;
type MockArrayResponse = [status: number, data?: any, headers?: object];
type MockObjectResponse = {
status: number;
data: any;
headers?: object;
config?: object;
};
type MockResponse = MockArrayResponse | MockObjectResponse;