Simple fetch mocking module for testing HTTP requests in Node.js and browser environments. Current stable version is 1.3.0. It allows spying on fetch calls with pattern matching on method and URL path, returning custom Response objects. Key differentiators: lightweight, zero dependencies, TypeScript-first with built-in types, and simple reset mechanism. Release cadence is low; package is stable and mature for its niche.
npm install fetch-spyNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates spying on a GET request to /api/users, checking the response and call count.
Include query parameters in the pathname pattern or manually check query in test.
Ensure FetchSpy.reset() is called in beforeEach or afterEach.
Consider using fetch-spy alongside a fetch polyfill or use vi.stubGlobal in Vitest.
Use import statements instead of require. If using TypeScript, set moduleResolution to 'node16' or 'bundler'.
Use 'import * as FetchSpy from 'fetch-spy'' or 'import { spy } from 'fetch-spy''.Ensure tsconfig has 'moduleResolution': 'node16' or 'bundler' and 'esModuleInterop': true.
Check that the pattern's method and pathname exactly match the fetch request; pathname matching is glob-based but case-sensitive.
No dependency data recorded yet.