NetworkInterceptor
The NetworkInterceptor intercepts and mocks outgoing fetch() calls. Useful for testing, offline scenarios, and CORS avoidance.
Quick start
const container = boot();
// Static mock
container.mockFetch("/api/users", { json: [{ name: "Alice" }] });
// Dynamic handler
container.onFetch(url => {
if (url.includes("/health")) return { json: { ok: true } };
return null; // pass through
});
MockResponse
interface MockResponse {
status?: number; // default: 200
headers?: Record<string, string>;
body?: string;
json?: unknown; // auto-serialized, overrides body
}
Container methods
| Method | Description |
|---|---|
mockFetch(pattern, response) | Register static mock (string or RegExp) |
onFetch(handler) | Register dynamic handler |
network.clear() | Remove all rules and handlers |
network.ruleCount | Number of static rules |
network.handlerCount | Number of dynamic handlers |
Matching order
- Static rules are checked first (first match wins)
- Dynamic handlers are checked in registration order
- If nothing matches, the request passes through to the real network
mockResponseToFetchResponse
Converts a MockResponse to a fetch-compatible object:
import { mockResponseToFetchResponse } from "@run0/jiki";
const resp = mockResponseToFetchResponse({ json: { ok: true } });
await resp.json(); // { ok: true }