Test with a fake backend
Test calls against a fake backend made from your endpoints.
This tutorial builds a fake backend from your endpoints and runs a call against it, so you can test your code without a network.
Effetch never touches the network itself: it sends every request through the
HttpClient you provide. In tests, @monarcode/effetch/test gives you a fake
one, made from your endpoints, so your code runs as it does in production:
the real client, with its plugins, defaultError and statusErrors.
Build a fake backend from your endpoints
FetchTest.layer takes a list of handlers, one per route, and is an
HttpClient layer. Provide it where FetchHttpClient.layer goes.
The following example defines three endpoints, answers each one from the fake backend, and gets a todo through the real client:
import * as from "effect/Effect";
import * as from "effect/Layer";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/test";
const = .({
: .,
: .,
: .,
});
const = .({ : ., : . });
class extends .<>()("TodoNotFound", {
: .,
}) {}
const = .("GET /todos/:id", {
: ,
: { 404: },
});
const = .("POST /todos", {
: ,
: ,
});
const = .("GET /todos", { : .() });
class extends .<>()("Api", {
: "https://api.test",
}) {}
class extends .<>()("TodoApi", , {
: ,
: ,
: ,
}) {}
const = .([
.(, ({ }) =>
// params are strings, as they are in the URL
.({
: (.),
: "Write docs",
: new (0),
}),
),
.(, ({ }) =>
.({ : 2, ... }),
),
.(, 503),
]);
const = ..(
.(.),
.(),
);
export const = .(function* () {
const = yield* ;
const = yield* .({ : { : 1 } });
// todo.due is a Date again: it went over the wire as a string
return ;
}).(.());What a handler gets and returns
What a handler gets and returns is typed by its endpoint, and checked as a real backend would check it:
paramshas the route's params, as strings.bodyis decoded with the endpoint'sinputschema, sobody.dueis aDatehere. A multipart body is read back from the form, with its files asFiles.queryis decoded with the endpoint's query schema, if it has one, and is the raw query, as strings, otherwise.- The result has
output's type, and is encoded withoutputbefore it's sent, so aDategoes over the wire as a string, as in production. WithFetch.text()orFetch.bytes(), it's sent as text or bytes.
A body, query or result that doesn't match its schema is a defect that names
the route: it's a bug in the code under test, or in the test. Handlers work
the same for definitions made with Fetch.endpoint, endpoints made with
client.endpoint or client.bind, and a Fetch.Endpoints class's
definitions.
Handlers that use services
A handler can use services, such as a clock or a store of fake data. The layer then needs them, and runs its handlers with the services it was built with, and those of the call they answer, which win when both have one.
Run the test
FetchTest doesn't depend on a test runner: a test is an Effect, so run it
with it.effect from @effect/vitest, or with Effect.runPromise in Jest,
Bun or Deno.
What's next
- Test errors and statuses: make the fake backend fail, or answer with another status.
- Check what was sent: assert on the requests your code sent, and see how routes match them.
- Test stream endpoints: answer a stream endpoint with a list of events or values.
- Use a hand-written HttpClient: fake a network failure or a slow response.