Test errors and statuses
Make the fake backend fail or answer with another status.
The error paths of your code are the hardest to reach against a real API: you can't ask a server for a 503 on demand. With the fake backend, you can answer any request with an error, or with a status other than 200, and check that your code handles each one. This page shows both.
Fail every request to an endpoint
To answer every request to an endpoint with an error, use FetchTest.fail,
with a status and an optional body:
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/test";
class extends .<>()("TodoNotFound", {
: .,
}) {}
const = .("GET /todos/:id", {
: { 404: },
});
const = .("GET /todos");
export const = .([
// The call fails with a TodoNotFound.
.(, 404, new ({ : "No todo 9" })),
// The call fails with an HttpError, status 503, asking to retry in 1 second.
.(, 503, , {
: { "retry-after": "1" },
}),
]);FetchTest.fail encodes the body as JSON with the first schema that
encodes it: the endpoint's statusErrors schemas for that status, then its
error schema. The client then decodes it back into your domain error, or
into HttpError.body. Without a body, the response is empty, so the call
fails with an HttpError.
A few more rules apply:
- The status must be from 300 to 599, since
failanswers with an error. - Headers go in the fourth argument, such as
retry-afterto test theretryplugin. - The client's maps. A
Fetch.endpointdefinition doesn't carry the client'sstatusErrorsordefaultError. Without schemas of its own, the body is sent by its runtime type, so an error class goes as JSON. The client still decodes it with its own maps, soFetchTest.fail(getTodo, 401, new SessionExpired(…))fails withSessionExpiredwhen only the client maps 401. You don't need to repeat the map on the endpoint. - A body that no schema encodes is a defect, since it's a bug in the test.
Answer with another status
To fail some requests and answer others, return FetchTest.reply(status, body) from a respond handler. The following backend answers 404 for
every todo but the first, and 201 with a location header when a todo is
created:
import * as from "effect/Effect";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/test";
const = .({ : ., : . });
const = .("GET /todos/:id", { : });
const = .("POST /todos", {
: .({ : . }),
: ,
});
export const = .([
.(, ({ }) =>
.(
. === "1"
? { : 1, : "Write docs" }
: .(404, { : `No todo ${.}` }),
),
),
.(
,
({ }) => .({ : 2, ... }),
{
: 201,
: { : "/todos/2" },
},
),
]);A reply's body isn't checked against the endpoint's schemas. It's sent as
it is, the way a call's body without input is:
undefinedas no body;- a string as text;
- bytes as bytes;
- a
Blob,FormDataorURLSearchParamsas it is; - anything else as JSON.
For a 2xx with a typed body, such as a 201, don't use reply. Pass
{ status: 201 } as respond's third argument instead, as createTodo
does, so the body is still encoded with output.
What's next
- Errors by status: declare the domain errors
that
FetchTest.failencodes. - Check what was sent: assert on the requests your code sent.
- Use a hand-written HttpClient: fake a network failure, which the fake backend doesn't do.