Testing
Use a hand-written HttpClient
Fake the transport with your own HttpClient for what a fake backend doesn't do.
This page shows how to provide an HttpClient of your own in tests, for what
a fake backend doesn't do, such as a network failure or a
slow response.
Provide your own HttpClient
Effetch sends every request through the HttpClient layer you provide, so a
test can provide one that answers however you need. The following client
records each request and answers with a todo:
import * as from "effect/Effect";
import * as from "effect/http/HttpClient";
import * as from "effect/http/HttpClientResponse";
import * as from "effect/Layer";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
const = .({ : ., : . });
class extends .<>()("Api", {
: "https://api.test",
}) {}
const : <string> = [];
const = .(
.,
.((, ) => {
.(`${.} ${.}`);
return .(
.(
,
.({ : 1, : "Write docs" }),
),
);
}),
);
const = .(function* () {
const = yield* ;
return yield* .("/todos/:id", { : { : 1 }, : });
});
const = await .(
.(.(..(.()))),
);
// todo: { id: 1, title: "Write docs" }; sent: ["GET https://api.test/todos/1"]What's next
- Test with a fake backend: answer requests from your endpoints, with typed handlers.
- Test stream endpoints: fake a stream whose whole body arrives at once.
- Platform notes: the
HttpClientsetup each platform needs.