---
url: https://haihv.github.io/valifetch/api/mock/functions/createMock.md
---

# Function: createMock()

> **createMock**(): [`ValifetchMock`](../../types/type-aliases/ValifetchMock.md)

Defined in: [src/mock/index.ts:120](https://github.com/haihv/valifetch/blob/285835d38a6676b98d2ee1a93e23166c6c834cb2/src/mock/index.ts#L120)

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`](../../types/type-aliases/ValifetchMock.md)

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
```
