Skip to content

Function: createMock() ​

createMock(): ValifetchMock

Defined in: src/mock/index.ts:120

Creates a valifetch mock for use in tests.

Register fixture responses by URL pattern and HTTP method, then attach the mock to a valifetch instance via extend({ hooks: mock.hooks }). Matched requests are intercepted before reaching the network; unmatched requests fall through to the real fetch.

Returns ​

ValifetchMock

A ValifetchMock with route registration, call inspection, and hooks.

Example ​

ts
import { createMock } from 'valifetch/mock';
import valifetch from 'valifetch';

const mock = createMock();
mock.get('/users').reply(200, [{ id: 1, name: 'Alice' }]);
mock.post('/users').reply(201, { id: 2, name: 'Bob' });

const api = valifetch.extend({ hooks: mock.hooks });

const users = await api.get('https://api.example.com/users');
// assert(users[0].name === 'Alice')

const call = mock.lastCall();
// assert(call?.method === 'GET')

mock.reset(); // clear between tests

Released under the MIT License. Built with Claude Code.