Skip to Content
Reference

FetchTest

Every member of the FetchTest namespace: the fake backend for tests and the requests it records.

@monarcode/effetch/test exports one name, the FetchTest namespace. Import it as follows:

import {  } from "@monarcode/effetch/test";

Its exports follow, grouped by the guide that covers them. Each one has the first paragraph of its docs, and what it needs and how it fails; your editor shows the rest, with an example, when you hover the name.

Fake backend

Guide: Test with a fake backend.

FetchTest.fail

Makes a handler that answers every request to endpoint with the error status status, for layer. To answer some requests with an error and others with data, return a reply from respond instead.

body is encoded as JSON with the first schema that encodes it: the endpoint's statusErrors schemas for status, then its error schema. So the client decodes it back into your domain error, or into HttpError.body. A definition made with Fetch.endpoint has no access to the client's statusErrors and defaultError, so without its own schemas, the body is sent by its runtime type (see reply). A body that no schema encodes is a defect. Without body, the response is empty.

Throws when endpoint isn't one, status isn't from 300 to 599, or a 304 has a body.

FetchTest.layer

Makes a fake backend: an HttpClient layer that answers requests with handlers, without a network. Provide it where FetchHttpClient.layer goes, under a real client with its plugins, so a test runs the same code as production, schemas included.

  • Matching. A request is answered by the handler whose route matches its method and path. Without options.baseUrl, a route matches the end of the path, so GET /todos/:id answers https://api.test/v1/todos/1. When several routes match, the longest wins, then the one with the most literal segments (/todos/count before /todos/:id), then the one listed first. A route with an absolute URL matches that URL only.
  • No match. A request no handler matches is a defect. Its message lists the routes the backend has, and the ones that match the path with another method.
  • Recording. The layer also provides the Recorder, with a new record each time it's built. Read it with requests.
  • Services. The layer needs the services its handlers need. A handler runs with the services the layer was built with, and those of the call it answers, which win when both have one.

FetchTest.reply

Makes a response that a respond handler sends as it is, for any status: a 201 with a location header, a 404 for a missing item, a 500. The body isn't checked against the endpoint's schemas.

Throws when the status isn't from 200 to 599, or when a 204, 205 or 304 reply has a body.

FetchTest.respond

Makes a handler that answers endpoint's requests with handler, for layer.

The handler succeeds with a value of output's Type, which is encoded with output before it's sent, so a Date goes over the wire as a string, as in production. With Fetch.text() or Fetch.bytes(), it's sent as text or bytes. A value that doesn't match output is a defect. Without output, the value is sent by its runtime type (see reply), and undefined sends no body.

Throws when endpoint isn't one, or options.status isn't from 200 to 599.

FetchTest.FailBody

The body fail takes for endpoint EP and status S: a value of the statusErrors schemas for S, or of the error schema.

  • On a bound endpoint, statusErrors has the endpoint's and the client's schemas, but not their statuses, so the body may be a value of any of them. Its error is always set, Schema.Unknown by default, which takes anything.
  • On a definition without error, a status without statusErrors takes any body, as the client's defaultError isn't known.

FetchTest.FailServices

The services a fail handler for endpoint EP needs: those of encoding its statusErrors and error schemas.

FetchTest.Handler

One route of the fake backend, made by respond or fail. Pass a list of them to layer.

  • R: the services it needs to run, which the layer then needs.

FetchTest.HandlerServices

The services handler H needs to run: R for a Handler<R>. For a union of handlers, the union of their services, which is what layer needs.

FetchTest.LayerOptions

Options for layer.

Prop

Type

FetchTest.QueryParams

The query parameters of a request, as they were in the URL (decoded): a string per key, or a list of strings for a key that was repeated.

FetchTest.Reply

A response sent as it is, made by reply. A respond handler returns one to answer with another status, such as 201 or 404.

FetchTest.ReplyOptions

Options for a reply or a fail handler.

Prop

Type

FetchTest.RequestBody

The body a handler gets for route R, whose endpoint has the input schema I:

  • undefined for a route whose method takes no body, such as GET;
  • the decoded body, typed by I, with an input schema;
  • unknown otherwise: the JSON value for a JSON body, the text for a text or form body, the bytes for other bytes, the FormData or stream as it was sent, and undefined without a body.

FetchTest.RequestQuery

The query a handler gets: decoded with the endpoint's query schema QS when it has one, as the call gave it, and the raw QueryParams otherwise.

FetchTest.RespondOptions

Options for the responses of a respond handler that aren't a Reply.

Prop

Type

FetchTest.RespondRequest

What a respond handler is called with, for one request to route R, whose endpoint has the input schema I and the query schema QS.

Prop

Type

FetchTest.RespondResult

What a respond handler succeeds with, for an endpoint with the output schema O and the stream format St, or a Reply:

  • without St: a value of output's Type, encoded with output before it's sent. Without output, the value is sent by its runtime type (see reply).
  • "sse": a list of TestEvents, sent as Server-Sent Events. An event or id with a line break can't be sent, and the call dies.
  • "ndjson": a list of values of output's Type (anything that has a JSON form without output), sent one per line. With Fetch.text(), a value with a line break, or a blank one, can't be sent as one line, and the call dies.

FetchTest.RespondServices

The services a respond handler for endpoint EP needs, besides its own: those of encoding output, and of decoding input and the query schema.

FetchTest.RouteParams

The path params a handler gets for route R: one key per :param, each a string, as it was in the URL (decoded).

FetchTest.TestEndpoint

An endpoint the fake backend can answer for: one made by Fetch.endpoint, api.endpoint or api.bind. The fake backend reads its route and its schemas.

FetchTest.TestEvent

One event a respond handler sends to an SSE endpoint (one with stream: "sse"): its data, of output's Type (a string without output), and optionally its event name and id.

Prop

Type

Recorded requests

Guide: Check what was sent.

FetchTest.Recorder

The service that keeps the requests the fake backend received, in the order it received them. layer provides it, with a new, empty record each time the layer is built. Read it with requests.

FetchTest.requests

The requests the fake backend received so far, oldest first.

It needs the Recorder, which layer provides. Under a client's layer, Layer.provide hides it, so when a test reads it, provide the fake backend with Layer.provideMerge instead.

FetchTest.SentRequest

A request the fake backend received, as the Recorder keeps it.

Prop

Type

  • Entry points: what each entry point exports.
  • Fetch: every member of the Fetch namespace.
  • Plugins: the built-in plugins and their options.
  • Compatibility: the Effect, runtime and TypeScript versions Effetch supports.

On this page