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
| Parameter | Type | Description |
|---|---|---|
url | string | The URL string to match. |
response | any | JSON-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 });