Registry / testing / msw-fetch-mock

msw-fetch-mock

JSON →
library0.6.1jsnpmunverified

An undici-style fetch mock API built on top of MSW (Mock Service Worker) for testing. Version 0.6.1 supports Node.js (>=18), browser, and native (no MSW dependency) environments via subpath exports. It provides a familiar fluent API similar to Cloudflare Workers' fetchMock or Node.js undici's MockAgent. Key differentiators: integrates seamlessly with MSW, supports both MSW v1 (via /legacy) and v2, and allows sharing existing MSW servers. Peer dependency on MSW ^1.0.0 or ^2.12.7. Active development with CI, weekly downloads ~500.

npm install msw-fetch-mock
INSTALL
IMPORT
SIG · MSW-FETCH-MOCK
M
msw-fetch-mock
testingjavascriptv0.6.1
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.

fetchMock
import { fetchMock } from 'msw-fetch-mock'
import fetchMock from 'msw-fetch-mock'
Default export does not exist; use named import.
createFetchMock
import { createFetchMock } from 'msw-fetch-mock'
const { createFetchMock } = require('msw-fetch-mock')
Library is ESM-only; require() will not work.
fetchMock (native)
import { fetchMock } from 'msw-fetch-mock/native'
import { fetchMock } from 'msw-fetch-mock'
Use /native subpath to avoid MSW dependency.

Shows basic setup and usage of fetchMock for mocking an HTTP GET request in a test suite, including activation, cleanup, and assertion.

import { fetchMock } from 'msw-fetch-mock'; beforeAll(() => fetchMock.activate({ onUnhandledRequest: 'error' })); afterAll(() => fetchMock.deactivate()); afterEach(() => { fetchMock.assertNoPendingInterceptors(); fetchMock.reset(); }); it('mocks GET request', async () => { fetchMock .get('https://api.example.com') .intercept({ path: '/users', method: 'GET' }) .reply(200, { users: [{ id: '1', name: 'Alice' }] }); const res = await fetch('https://api.example.com/users'); const data = await res.json(); expect(data.users).toHaveLength(1); });
Debug
Known issues
gotchaOnly one MSW server can be active at a time. Using standalone activate() while another server is listening will throw an error.
fix
Use createFetchMock(server) to share an existing MSW server.
affects: >=0.0.0
deprecatedMSW v1 is supported via /legacy subpath but is considered legacy; MSW v1 is no longer actively maintained.
fix
Upgrade to MSW v2 and use the main entry or /node subpath.
affects: >=0.0.0
gotchaUnhandled requests in 'error' mode cause fetch() to reject, including requests to consumed one-time interceptors.
fix
Set onUnhandledRequest to 'warn' or 'bypass' if needed.
affects: >=0.0.0
breakingRequires Node.js >= 18. Older versions are not supported.
fix
Update Node.js to 18+.
affects: >=0.6.0
deprecatedThe standalone import 'msw-fetch-mock' may be deprecated in future in favor of explicit subpaths.
fix
Use explicit subpaths like 'msw-fetch-mock/node' for clarity.
affects: >=0.6.0
Errors
Common errors & fixes
Error: [MSW] Failed to activate: Another server is already listening.
Activating fetchMock standalone while an MSW server is already active.
fix
Use createFetchMock(server) to share the existing server.
ERR_REQUIRE_ESM: require() of ES Module not supported.
Using CommonJS require() to import the ESM-only package.
fix
Use import syntax or set type: 'module' in package.json.
TypeError: fetchMock.activate is not a function
Incorrect import; likely using default import instead of named import.
fix
Use import { fetchMock } from 'msw-fetch-mock'.
Error: fetchMock is not defined
Using /native subpath but MSW is not installed, or incorrect import path.
fix
Install msw or use import { fetchMock } from 'msw-fetch-mock/native'.
Upgrade
Version history
0.6.1latest on npm
Audit
Dependencies
mswoptionalpeer dependency; used for request interception in Node.js and browser environments
Agent activity
4 hits · last 30 days
node
4
Resources
msw-fetch-mock — npm install msw-fetch-mock · libregistry