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-requestsNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to set up static and dynamic mocks with activateMocks, and how requests are intercepted without source code changes.
If upgrading from pre-1.0.0, replace all calls to `mockRequest()` with `activateMocks()` and change imports to ESM.
Upgrade Node.js to >=16.11.0 and npm >=8.
Use `deactivateMocks()` to restore real network behavior, or scope mocks by condition (e.g., Node environment check).
Ensure your mock URL templates include `:param` placeholders for dynamic segments, or parse URL manually.
Use string patterns with path parameters instead of regex.
Ensure you pass an array of valid MockRequest objects: `activateMocks([{ url: '...', method: 'GET', response: ... }])`.Use `import { activateMocks } from 'mock-requests'` instead of `require`. If using CommonJS in Node, enable ESM or use dynamic import.Use a bundler (Webpack, Rollup, Vite) that handles ESM, or set `"type": "module"` in package.json.