Testing
Test stream endpoints
Fake a streamed response with a list of events or values.
This page shows how to answer a stream endpoint from the fake backend, so you can test code that reads Server-Sent Events or newline-delimited JSON.
Answer with a list
A handler for a stream endpoint returns a list, and FetchTest sends it as
the stream's body:
- For
stream: "sse", the list holds events, each with itsdataand an optionaleventandid. - For
stream: "ndjson", the list holds values.
Each value is encoded with the endpoint's output. The following backend
sends two events for a job:
import * as from "effect/Effect";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/test";
const = .({ : . });
const = .("GET /jobs/:id/events", {
: ,
: "sse",
});
export const = .([
.(, ({ }) =>
.([
{ : { : 50 } },
{ : "done", : ., : { : 100 } },
]),
),
]);Test slow or broken streams
The whole body is sent at once. To test how your code handles values that
arrive slowly, or a body that breaks off, use
a hand-written HttpClient whose Response
has a ReadableStream body.
What's next
- Streaming responses: how stream endpoints read events and values, and how a stream fails.
- Use a hand-written HttpClient: send a body that arrives slowly or breaks off.
- Check what was sent: assert on the requests your code sent.