Registry / testing / mock-requests

mock-requests

JSON →
library1.4.3jsnpmunverified

Mocks XMLHttpRequest and fetch requests with static or dynamic responses without changing source code. Current stable version 1.4.3. Release cadence appears low (latest 2022). Differentiators: no source code modification needed, supports both XHR and fetch, dynamic responses based on payload/query parameters, delay simulation, and Webpack plugin for production-like mocking. Unlike alternatives like fetch-mock or nock, it intercepts at the browser level and works with both APIs seamlessly.

npm install mock-requests
INSTALL
IMPORT
SIG · MOCK-REQUESTS
M
mock-requests
testingjavascriptv1.4.3
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.

activateMocks
import { activateMocks } from 'mock-requests'
const activateMocks = require('mock-requests').activateMocks
ESM only; CommonJS require will not work because the package is ESM-only.
MockRequest
import { MockRequest } from 'mock-requests'
import MockRequest from 'mock-requests'
Named export, not default.
types
import type { MockRequestOptions } from 'mock-requests'
import { MockRequestOptions } from 'mock-requests'
Type-only imports should use `import type` for tree-shaking; package ships TypeScript definitions.

Shows how to set up static and dynamic mocks with activateMocks, and how requests are intercepted without source code changes.

// Activate mock responses globally for all fetch and XMLHttpRequest calls import { activateMocks, MockRequest } from 'mock-requests'; // Define mock responses const mockRequests: MockRequest[] = [ { url: 'https://api.example.com/users', method: 'GET', response: { status: 200, body: { id: 1, name: 'John' } }, }, { url: 'https://api.example.com/users/:id', method: 'GET', response: (req) => { const id = req.params?.id; return { status: 200, body: { id, name: `User ${id}` } }; }, }, ]; // Activate mocks (call once at app entry) if (process.env.NODE_ENV === 'development') { activateMocks(mockRequests); } // Now all requests to those URLs will be intercepted const response = await fetch('https://api.example.com/users'); const data = await response.json(); console.log(data); // { id: 1, name: 'John' } // With dynamic response const response2 = await fetch('https://api.example.com/users/42'); const data2 = await response2.json(); console.log(data2); // { id: '42', name: 'User 42' }
Debug
Known issues
breakingVersion 1.0.0 changed the API from `mockRequest()` to `activateMocks()` and introduced ESM-only.
fix
If upgrading from pre-1.0.0, replace all calls to `mockRequest()` with `activateMocks()` and change imports to ESM.
affects: >=1.0.0 <1.0.0
breakingVersion 1.0.0 dropped support for Node <16.11.0 and requires npm >=8.
fix
Upgrade Node.js to >=16.11.0 and npm >=8.
affects: >=1.0.0
gotchaMocks are global; activating in one file affects all fetch/XHR calls in the application.
fix
Use `deactivateMocks()` to restore real network behavior, or scope mocks by condition (e.g., Node environment check).
affects: >=1.0.0
gotchaDynamic response functions receive a request object with `params`, `query`, `body`, etc., but `params` only populated when URL contains path parameters like `:id`.
fix
Ensure your mock URL templates include `:param` placeholders for dynamic segments, or parse URL manually.
affects: >=1.0.0
deprecatedThe `MockRequest.url` field supports regex patterns but this is undocumented and may be removed in future versions.
fix
Use string patterns with path parameters instead of regex.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'url')
Calling `activateMocks()` without passing an array of MockRequest objects, or passing an empty array with invalid entries.
fix
Ensure you pass an array of valid MockRequest objects: `activateMocks([{ url: '...', method: 'GET', response: ... }])`.
Error: activateMocks is not a function
Using CommonJS `require()` on an ESM-only package, or bundler not resolving ESM correctly.
fix
Use `import { activateMocks } from 'mock-requests'` instead of `require`. If using CommonJS in Node, enable ESM or use dynamic import.
SyntaxError: Unexpected token 'export'
Attempting to use the package in a CommonJS environment without transpilation.
fix
Use a bundler (Webpack, Rollup, Vite) that handles ESM, or set `"type": "module"` in package.json.
Upgrade
Version history
1.4.3latest on npm
Audit
Dependencies
noderequiredRequires Node.js >=16.11.0 for built-in fetch in Node.
Agent activity
4 hits · last 30 days
node
4
Resources
mock-requests — npm install mock-requests · libregistry