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-mockNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows setup of createFetchMock with vi, enabling mocks, and a simple test with mockResponse and assertions.
Use v0.2.2 for Node <18 or Vitest 1.x.
Remove any arguments from enableMocks() call.
Use mockRejectOnce with a new Error(), or mockResponse with status 500.
Replace mockOnce() with mockResponseOnce().
Call fetchMocker.enableMocks() in setup file or beforeAll.
Call fetchMocker.enableMocks() after createFetchMock(vi).
Ensure test files are run by Vitest, e.g., npx vitest run.
Upgrade to Node >=18 or install vitest-fetch-mock@0.2.2.
Use vitest-fetch-mock@0.2.2 for Vitest 1.x, or upgrade Vitest to >=2.