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-adapterNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to mock a GET request with axios-mock-adapter, including import, mock setup, and response handling.
Upgrade axios to >= 0.17.0
Use import syntax or enable CommonJS interop in bundler
Use .replyOnce() directly
Call mock.restore() in afterEach or teardown
Ensure params object includes all expected keys
Use const AxiosMockAdapter = require('axios-mock-adapter').default; or use import syntaxPass an existing axios instance or import axios as const mock = new AxiosMockAdapter(axios);
Use .reply() instead of .networkError() if you intend to return a success response
Increase delayResponse or remove timeout mock