{"id":47618,"library":"fetch-mock-one.com","title":"fetch-mock","description":"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.","status":"active","version":"7.3.1-fork-0.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/One-com/fetch-mock","tags":["javascript","fetch","http","mock","testing","spy","xhr","ajax"],"install":[{"cmd":"npm install fetch-mock-one.com","lang":"bash","label":"npm"},{"cmd":"yarn add fetch-mock-one.com","lang":"bash","label":"yarn"},{"cmd":"pnpm add fetch-mock-one.com","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required in Node.js environment because Node does not have a native fetch API (pre-v18).","package":"node-fetch","optional":true}],"imports":[{"note":"fetch-mock is an ESM package; CommonJS require() works in Node but may cause issues with some bundlers.","wrong":"const fetchMock = require('fetch-mock');","symbol":"default","correct":"import fetchMock from 'fetch-mock';"},{"note":"Default export is the function; named import is incorrect.","wrong":"const { fetchMock } = require('fetch-mock');","symbol":"fetchMock","correct":"import fetchMock from 'fetch-mock';"},{"note":"MockResponse is a named export, not default.","wrong":"import MockResponse from 'fetch-mock';","symbol":"MockResponse","correct":"import { MockResponse } from 'fetch-mock';"}],"quickstart":{"code":"import fetchMock from 'fetch-mock';\nimport fetch from 'node-fetch';\n\nfetchMock.mock('http://example.com', { body: { ok: true }, status: 200 });\nconst res = await fetch('http://example.com');\nconst data = await res.json();\nconsole.log(data); // { ok: true }\nfetchMock.restore();","lang":"typescript","description":"Shows basic mocking of a GET request to http://example.com using fetch-mock."},"warnings":[{"fix":"Replace fetchMock() with fetchMock.mock() or pass options to the constructor.","message":"In v7, the default export is no longer callable; use fetchMock.mock() instead of fetchMock().","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Use fetchMock.reset() to clear routes; use fetchMock.restore() only to remove global fetch mock.","message":"In v7, .restore() does not reset the sandbox; use .reset() to clear all routes.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Install node-fetch and ensure it's available globally or import fetch from 'node-fetch' before using fetch-mock.","message":"fetch-mock does not polyfill fetch; you must provide a fetch implementation in Node.js (e.g., node-fetch).","severity":"gotcha","affected_versions":"*"},{"fix":"Replace .once(url, response) with .mock(url, response, { repeat: 1 }).","message":"Using .once() to limit call count is deprecated; use .mock({...}, {repeat: 1}) instead.","severity":"deprecated","affected_versions":">=7.0.0"},{"fix":"Always call sandbox.restore() after tests if using sandbox mode.","message":"When using sandbox mode, fetchMock.restore() does not clean up the sandbox; call sandbox.restore() separately.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"search_vec":"'0.1.0':25 '7.3.1':23 '8':32 'ajax':83 'allow':12 'altern':55 'api':11,39 'bodi':46 'browser':34 'compar':53 'comprehens':5 'easili':15 'fetch':2,10,61,69,77 'fetch-mock':1,60 'fluent':38 'focus':66 'fork':24,26 'global':51 'header':44 'http':17,78 'javascript':76 'librari':7 'lightweight':64 'like':56 'maintain':27 'match':41 'method':43 'mock':3,6,16,62,70,79 'mode':52 'msw':59 'need':72 'nock':57 'node.js':31 'one.com':29 'provid':36 'request':18 'sandbox':49 'servic':74 'sole':67 'spi':81 'support':30 'test':21,80 'unit':20 'url':42 'version':22 'without':71 'worker':75 'xhr':82","created_at":"2026-06-07T16:52:26.442978+00:00","updated_at":"2026-06-07T16:52:26.442978+00:00","problems":[{"fix":"Update to v7 and use fetchMock.mock() or use fetchMock() in older versions.","cause":"Using an older version where fetchMock() was the main API.","error":"TypeError: fetchMock.mock is not a function"},{"fix":"Install node-fetch and add global.fetch = require('node-fetch') at the top of your test file.","cause":"No fetch polyfill in Node.js environment.","error":"Error: fetch is not defined"},{"fix":"Do not call fetchMock.mock() after restore; use fetchMock.reset() to clear routes without unmocking fetch.","cause":"Attempting to call fetchMock.mock() after fetchMock.restore() has already been called.","error":"Uncaught Error: fetchMock already mocked"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"http://www.wheresrhys.co.uk/fetch-mock","github":"https://github.com/One-com/fetch-mock","docs":null,"changelog":null,"pypi":null,"npm":"fetch-mock-one.com","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}