Registry / testing / vitest-fetch-mock

vitest-fetch-mock

JSON →
library0.4.5jsnpmunverified

Vitest Fetch Mock (v0.4.5) is a fetch mocking library for Vitest, forked from jest-fetch-mock. It mocks the global fetch API using Vitest's vi.fn() under the hood, providing a simple API to mock responses, inspect calls, and reset state. Works with Vitest >=2.0.0 and Node >=18. Unlike nock or msw, it directly replaces global.fetch without interceptors or service workers. Ships TypeScript types.

npm install vitest-fetch-mock
INSTALL
IMPORT
SIG · VITEST-FETCH-MOCK
V
vitest-fetch-mock
testingjavascriptv0.4.5
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.

createFetchMock
import createFetchMock from 'vitest-fetch-mock';
Default export function. Must pass vi instance from vitest.
fetchMock
fetchMock.mockResponse(JSON.stringify({ok: true}));
vi.mock('fetch'); fetch.mockResponse(...)
fetchMock is a global set after calling enableMocks().
globalThis.fetch
globalThis.fetch = fetchMock;
global.fetch = fetchMock;
Uses globalThis for modern environments; avoid global.

Shows setup of createFetchMock with vi, enabling mocks, and a simple test with mockResponse and assertions.

import createFetchMock from 'vitest-fetch-mock'; import { vi, describe, it, expect } from 'vitest'; const fetchMocker = createFetchMock(vi); fetchMocker.enableMocks(); describe('fetch tests', () => { it('should mock fetch', async () => { fetchMock.mockResponse(JSON.stringify({ data: 'test' })); const response = await fetch('/api'); const body = await response.json(); expect(body.data).toBe('test'); expect(fetchMock).toHaveBeenCalledTimes(1); }); });
Debug
Known issues
breakingIn v0.4.0, fetch mock switched from polyfill to global fetch, requires Node >=18. Older Node versions break.
fix
Use v0.2.2 for Node <18 or Vitest 1.x.
affects: >=0.4.0
breakingenableMocks() no longer accepts arguments; previously (v0.2.2) could pass a custom fetch implementation.
fix
Remove any arguments from enableMocks() call.
affects: >=0.4.0
gotchafetchMock.mockReject() does not return a valid Response object; calls to .json() or .text() will fail.
fix
Use mockRejectOnce with a new Error(), or mockResponse with status 500.
affects: >=0.1.0
deprecatedmockOnce() is deprecated; use mockResponseOnce() instead.
fix
Replace mockOnce() with mockResponseOnce().
affects: >=0.4.0
gotchaIf you forget to call enableMocks(), global fetch remains unmocked and tests may hit real network.
fix
Call fetchMocker.enableMocks() in setup file or beforeAll.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: fetchMock.mockResponse is not a function
fetchMock global not initialized because enableMocks() not called or imported incorrectly.
fix
Call fetchMocker.enableMocks() after createFetchMock(vi).
ReferenceError: vi is not defined
Using vitest-fetch-mock without Vitest running (e.g., in plain Node test).
fix
Ensure test files are run by Vitest, e.g., npx vitest run.
Error: fetch is not a function
Node version <18 lacks global fetch; vitest-fetch-mock v0.4+ requires Node >=18.
fix
Upgrade to Node >=18 or install vitest-fetch-mock@0.2.2.
Vitest 2.x peer dependency not met
Installed with Vitest 1.x which is incompatible with v0.4+.
fix
Use vitest-fetch-mock@0.2.2 for Vitest 1.x, or upgrade Vitest to >=2.
Upgrade
Version history
0.4.5latest on npm
Audit
Dependencies
vitestrequiredPeer dependency; required to utilize vi.fn() and mock API
Agent activity
8 hits · last 30 days
node
8
Resources
vitest-fetch-mock — npm install vitest-fetch-mock · libregistry