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.comNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows basic mocking of a GET request to http://example.com using fetch-mock.
Replace fetchMock() with fetchMock.mock() or pass options to the constructor.
Use fetchMock.reset() to clear routes; use fetchMock.restore() only to remove global fetch mock.
Install node-fetch and ensure it's available globally or import fetch from 'node-fetch' before using fetch-mock.
Replace .once(url, response) with .mock(url, response, { repeat: 1 }).Always call sandbox.restore() after tests if using sandbox mode.
Update to v7 and use fetchMock.mock() or use fetchMock() in older versions.
Install node-fetch and add global.fetch = require('node-fetch') at the top of your test file.Do not call fetchMock.mock() after restore; use fetchMock.reset() to clear routes without unmocking fetch.