Registry / testing / axios-mock-adapter-path-params

axios-mock-adapter-path-params

JSON →
library1.20.0jsnpmunverified

Fork of axios-mock-adapter that merges support for mocking requests with specific URL path parameters. v1.20.0 is the current stable release, with periodic updates. Key differentiator: allows matching requests based on path parameters like /users/:id, which the original adapter lacks. Supports ESM and CJS, includes TypeScript definitions, works in Node and browser, compatible with axios >= 0.9.0. Ideal for unit and integration testing of axios-based HTTP calls.

npm install axios-mock-adapter-path-params
INSTALL
IMPORT
SIG · AXIOS-MOCK-ADAPTER
A
axios-mock-adapter-path-params
testingjavascriptv1.20.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.

MockAdapter
import MockAdapter from 'axios-mock-adapter-path-params'
const MockAdapter = require('axios-mock-adapter-path-params')
Default export; works with CommonJS require but ESM import is recommended.
MockAdapter
const MockAdapter = require('axios-mock-adapter-path-params');
const { MockAdapter } = require('axios-mock-adapter-path-params');
CommonJS require works; do not destructure named export.
MockAdapter
import MockAdapter from 'axios-mock-adapter-path-params'; import axios from 'axios'; const mock = new MockAdapter(axios);
import { MockAdapter } from 'axios-mock-adapter-path-params';
Default import only, not named.

Demonstrates mocking GET requests with query params and path params, using regex and reply function, then restoring mock.

import axios from 'axios'; import MockAdapter from 'axios-mock-adapter-path-params'; const mock = new MockAdapter(axios); mock.onGet('/users', { params: { searchText: 'John' } }).reply(200, { users: [{ id: 1, name: 'John Smith' }] }); mock.onGet(/\/users\/\d+/).reply(config => { return [200, { id: 1, name: 'Jane' }]; }); axios.get('/users', { params: { searchText: 'John' } }).then(response => { console.log(response.data); }); mock.onGet('/users/123').reply(200, { id: 123, name: 'Bob' }); axios.get('/users/123').then(response => { console.log(response.data); }); mock.restore();
Debug
Known issues
gotchaonGet with params object only matches exact query params; extra params cause mismatch.
fix
Use a function in reply to handle partial parameter matching, or mock with regex.
affects: >=0.0.0
gotchamock.restore() must be called to remove adapter; otherwise subsequent tests may be affected.
fix
Call mock.restore() in afterEach or test teardown.
affects: >=0.0.0
gotchaHistory is not automatically cleared between tests; call mock.reset() to clear handlers and history.
fix
Use mock.reset() in setup/teardown to avoid test pollution.
affects: >=0.0.0
gotchaWhen using TypeScript, you must have axios types installed to get full type support.
fix
Install @types/axios if not already present in your project.
affects: >=0.0.0
breakingVersion 1.x may not be compatible with axios 0.x due to changes in internal adapter structure.
fix
Ensure axios >=0.9.0, but test with your specific version. If issues, pin to compatible versions.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Network Error
Mock adapter's networkError() was invoked for a request that didn't have a matching handler.
fix
Ensure the request URL/method matches exactly, or add a fallback handler with .onAny().reply()
TypeError: Cannot read properties of undefined (reading 'apply')
Mock adapter is not correctly instantiated; probably imported incorrectly (destructured instead of default).
fix
Use 'import MockAdapter from ...' or 'const MockAdapter = require(...)'
Error: Request failed with status code 404
No mock handler matched the request; adapter returns 404 by default for unhandled requests.
fix
Add a matching handler or use .onAny().reply() to catch all requests.
Upgrade
Version history
1.20.0latest on npm
Audit
Dependencies
axiosrequiredPeer dependency; required at runtime to mock requests.
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
axios-mock-adapter-path-params — npm install axios-mock-adapter-path-params · libregistry