Skip to content

Type Alias: MockHandler ​

MockHandler = object

Defined in: src/mock/types.ts:29

Builder returned by mock.when() and the HTTP method shorthands. Call reply or replyOnce (chainable) to register fixture responses.

Methods ​

reply() ​

reply(status, body?, headers?): MockHandler

Defined in: src/mock/types.ts:42

Register a fixture response for all matching requests. If multiple reply/replyOnce calls are made on the same handler, responses are consumed in order (queue). This response stays at the end of the queue and is reused once all preceding replyOnce responses are consumed.

Parameters ​

status ​

number

HTTP status code

body? ​

unknown

Response body — always serialised via JSON.stringify (objects, arrays, strings, null). Pass null to produce a JSON null response. Omit entirely only for genuinely bodyless statuses (e.g. 204 No Content) — omitting on a 200/201 response will cause valifetch's JSON parser to throw.

headers? ​

HeadersInit

Additional response headers

Returns ​

MockHandler

The same MockHandler, for chaining additional reply/replyOnce calls.


replyOnce() ​

replyOnce(status, body?, headers?): MockHandler

Defined in: src/mock/types.ts:55

Register a fixture response that is consumed on the first matching request only. Subsequent requests fall through to the next registered response (or the real fetch if none remain). Mock replies are produced by a beforeRequest hook outside the retry loop, so the retry option does not retry a mocked response; each call consumes one queued reply.

Parameters ​

status ​

number

HTTP status code

body? ​

unknown

Response body — always serialised via JSON.stringify (objects, arrays, strings, null). Pass null to produce a JSON null response. Omit entirely only for genuinely bodyless statuses (e.g. 204 No Content) — omitting on a 200/201 response will cause valifetch's JSON parser to throw.

headers? ​

HeadersInit

Additional response headers

Returns ​

MockHandler

The same MockHandler, for chaining additional reply/replyOnce calls.

Released under the MIT License. Built with Claude Code.