Registry / testing / fetch-spy

fetch-spy

JSON →
library1.3.0jsnpmunverified

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-spy
INSTALL
IMPORT
SIG · FETCH-SPY
F
fetch-spy
testingjavascriptv1.3.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.

FetchSpy
import * as FetchSpy from 'fetch-spy'
import FetchSpy from 'fetch-spy'
Module uses namespace export. Default import is not available.
spy
import { spy } from 'fetch-spy'
const { spy } = require('fetch-spy')
Supports both named import and namespace import. The function is also available as FetchSpy.spy.
reset
import { reset } from 'fetch-spy'
Resets all spies; should be called in beforeEach or afterEach.
Spy
import type { Spy } from 'fetch-spy'
import { Spy } from 'fetch-spy'
Spy is a type, not a value. Use type import in TypeScript.

Demonstrates spying on a GET request to /api/users, checking the response and call count.

import * as FetchSpy from 'fetch-spy'; beforeEach(() => { FetchSpy.reset(); }); it('should spy on fetch', async () => { const spy = FetchSpy.spy( { method: 'GET', pathname: '/api/users' }, new Response(JSON.stringify({ id: 1, name: 'Alice' }), { status: 200 }) ); const res = await fetch('/api/users'); const data = await res.json(); expect(data).toEqual({ id: 1, name: 'Alice' }); expect(spy.calls.length).toBe(1); expect(spy.calls[0].url).toBe('/api/users'); });
Debug
Known issues
gotchaSpy patterns use pathname matching with glob support, but does not match query strings. Use URL pattern for query parameters.
fix
Include query parameters in the pathname pattern or manually check query in test.
affects: >=1.0.0
gotchaReset must be called to avoid leakage between tests; otherwise spy calls accumulate.
fix
Ensure FetchSpy.reset() is called in beforeEach or afterEach.
affects: >=1.0.0
gotchaThe module does not mock fetch globally by default; it only spies on calls. Use global fetch mocking if you need to prevent actual network requests.
fix
Consider using fetch-spy alongside a fetch polyfill or use vi.stubGlobal in Vitest.
affects: >=1.0.0
breakingIn version 1.x, the package changed from CommonJS to ESM-only. require() will not work.
fix
Use import statements instead of require. If using TypeScript, set moduleResolution to 'node16' or 'bundler'.
affects: >=1.0.0
Errors
Common errors & fixes
FetchSpy.spy is not a function
Incorrect import style: using default import instead of namespace or named import.
fix
Use 'import * as FetchSpy from 'fetch-spy'' or 'import { spy } from 'fetch-spy''.
Cannot find module 'fetch-spy' or its corresponding type declarations.
Missing TypeScript types due to incorrect import or module resolution.
fix
Ensure tsconfig has 'moduleResolution': 'node16' or 'bundler' and 'esModuleInterop': true.
Spy calls array is empty but fetch was called
Spy pattern does not match the actual fetch call (method or pathname mismatch).
fix
Check that the pattern's method and pathname exactly match the fetch request; pathname matching is glob-based but case-sensitive.
Upgrade
Version history
1.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
fetch-spy — npm install fetch-spy · libregistry