Registry / testing / fetch-mock-one.com

fetch-mock-one.com

JSON →
library7.3.1-fork-0.1.0jsnpmunverified

A comprehensive mocking library for the Fetch API, allowing you to easily mock HTTP requests in unit tests. Version 7.3.1-fork-0.1.0 (fork maintained by One.com) supports Node.js 8+ and browsers. It provides a fluent API for matching URLs, methods, headers, and bodies, with both sandbox and global modes. Compared to alternatives like nock or msw, fetch-mock is lightweight and focused solely on fetch mocking without needing a service worker.

npm install fetch-mock-one.com
INSTALL
IMPORT
SIG · FETCH-MOCK-ONE.COM
F
fetch-mock-one.com
testingjavascriptv7.3.1-fork-0.1.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.

default
✓ import fetchMock from 'fetch-mock';
✗ const fetchMock = require('fetch-mock');
fetch-mock is an ESM package; CommonJS require() works in Node but may cause issues with some bundlers.
fetchMock
✓ import fetchMock from 'fetch-mock';
✗ const { fetchMock } = require('fetch-mock');
Default export is the function; named import is incorrect.
MockResponse
✓ import { MockResponse } from 'fetch-mock';
✗ import MockResponse from 'fetch-mock';
MockResponse is a named export, not default.

Shows basic mocking of a GET request to http://example.com using fetch-mock.

import fetchMock from 'fetch-mock'; import fetch from 'node-fetch'; fetchMock.mock('http://example.com', { body: { ok: true }, status: 200 }); const res = await fetch('http://example.com'); const data = await res.json(); console.log(data); // { ok: true } fetchMock.restore();
Debug
Known issues
breakingIn v7, the default export is no longer callable; use fetchMock.mock() instead of fetchMock().
fix
Replace fetchMock() with fetchMock.mock() or pass options to the constructor.
affects: >=7.0.0
breakingIn v7, .restore() does not reset the sandbox; use .reset() to clear all routes.
fix
Use fetchMock.reset() to clear routes; use fetchMock.restore() only to remove global fetch mock.
affects: >=7.0.0
gotchafetch-mock does not polyfill fetch; you must provide a fetch implementation in Node.js (e.g., node-fetch).
fix
Install node-fetch and ensure it's available globally or import fetch from 'node-fetch' before using fetch-mock.
affects: *
deprecatedUsing .once() to limit call count is deprecated; use .mock({...}, {repeat: 1}) instead.
fix
Replace .once(url, response) with .mock(url, response, { repeat: 1 }).
affects: >=7.0.0
gotchaWhen using sandbox mode, fetchMock.restore() does not clean up the sandbox; call sandbox.restore() separately.
fix
Always call sandbox.restore() after tests if using sandbox mode.
affects: *
Errors
Common errors & fixes
TypeError: fetchMock.mock is not a function
Using an older version where fetchMock() was the main API.
fix
Update to v7 and use fetchMock.mock() or use fetchMock() in older versions.
Error: fetch is not defined
No fetch polyfill in Node.js environment.
fix
Install node-fetch and add global.fetch = require('node-fetch') at the top of your test file.
Uncaught Error: fetchMock already mocked
Attempting to call fetchMock.mock() after fetchMock.restore() has already been called.
fix
Do not call fetchMock.mock() after restore; use fetchMock.reset() to clear routes without unmocking fetch.
Upgrade
Version history
7.3.1-fork-0.1.0latest on npm
Audit
Dependencies
node-fetchoptionalRequired in Node.js environment because Node does not have a native fetch API (pre-v18).
Agent activity
4 hits · last 30 days
node
4
Resources
fetch-mock-one.com — npm install fetch-mock-one.com · libregistry