axios-mock-adapter is a testing library that intercepts axios HTTP requests and returns mocked responses without making real network calls.
The tool solves the problem of testing code that depends on HTTP requests by replacing axios's default adapter with one that matches requests against registered mock handlers and returns predefined responses. Developers define mocks by chaining methods like onGet, onPost, or onAny to specify which requests to intercept, what conditions they must match (URL patterns, parameters, request bodies), and what responses to return. The adapter can simulate various scenarios including successful responses, network errors, timeouts, and redirects. Requests that do not match any handler are rejected with a 404 response by default, though this behavior can be customized. The tool also tracks request history, allowing tests to verify that expected calls were made.
The tool suits developers writing unit and integration tests for JavaScript applications that use axios. It works in both Node.js and browser environments with axios versions from 0.17.0 onward. The project is installed as a development dependency. Developers should choose this tool when they want to avoid making real network requests during testing and need fine-grained control over mock behavior. The README does not compare it to alternative mocking libraries.
The project maintains active engagement with pull requests and issues, addressing user-reported problems and feature requests. The codebase receives regular updates to support new axios versions and testing patterns. The maintainers have added features like asymmetric matchers for flexible request matching, Promise-based reply handlers for composing responses from multiple sources, and the onNoMatch configuration option for controlling default behavior. The tool includes comprehensive documentation with numerous examples covering common testing scenarios.