Skip to content

dependabit / test-utils/src / mockFetch

Function: mockFetch() ​

ts
function mockFetch(url, response): void;

Defined in: packages/test-utils/src/mocks.ts:76

Replaces the global fetch with a Vitest mock that returns response as JSON when url is requested.

Parameters ​

ParameterTypeDescription
urlstringThe URL string to match.
responseanyJSON-serialisable response body.

Returns ​

void

Remarks ​

This mutates global.fetch; reset it in afterEach with vi.restoreAllMocks().

Example ​

ts
mockFetch('/api/users', { success: true });
const res = await fetch('/api/users');
expect(res.json()).resolves.toEqual({ success: true });

Released under the MIT License.