Skip to content

faultyFetch

faultyFetch(inner, options): {(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

Defined in: src/testing/index.ts:175

Wraps a fetch so some calls fail on purpose, for tests of how a client copes.

Each call that match accepts (all of them, by default) takes two draws from the generator, in this order: first one that decides whether the call gets a fault (draw < rate), then one that picks which of faults. Both are always taken, so the pick sequence doesn’t shift when rate changes. A call match rejects takes none. With a seed, the sequence is the same every run.

latencyMs uses the global setTimeout at call time, so fake timers work, and waits its full length even past the 2^31 - 1 ms a plain timer allows. Both waits end with the signal’s reason as soon as the request’s signal aborts, and any fault rejects with it at once when the signal is already aborted.

{(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

FaultyFetchOptions

(input, init?): Promise<Response>

MDN Reference

URL | RequestInfo

RequestInit

Promise<Response>

(input, init?): Promise<Response>

MDN Reference

string | URL | Request

RequestInit

Promise<Response>

const client = HttpClient.create({
fetch: faultyFetch(fetch, { faults: [{ status: 503, retryAfter: 1 }], seed: 42, rate: 0.5 }),
});

when inner or options is invalid, including headers a Response would refuse.