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. Its members:
import { } from "@monarcode/effetch/test";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.
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, soGET /todos/:idanswershttps://api.test/v1/todos/1. When several routes match, the longest wins, then the one with the most literal segments (/todos/countbefore/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 withrequests. - 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.
FetchTest.respond
Makes a handler that answers endpoint's requests with handler, for layer.
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,
statusErrorshas the endpoint's and the client's schemas, but not their statuses, so the body may be a value of any of them. Itserroris always set,Schema.Unknownby default, which takes anything. - On a definition without
error, a status withoutstatusErrorstakes any body, as the client'sdefaultErrorisn'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.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 ofoutput'sType, encoded withoutputbefore it's sent. Withoutoutput, the value is sent by its runtime type (seereply). "sse": a list ofTestEvents, sent as Server-Sent Events. Aneventoridwith a line break can't be sent, and the call dies."ndjson": a list of values ofoutput'sType(anything that has a JSON form withoutoutput), sent one per line. WithFetch.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, client.endpoint or client.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.
FetchTest.RequestBody
The body a handler gets for route R, whose endpoint has the input schema I:
undefinedfor a route whose method takes no body, such asGET;- the decoded body, typed by
I, with aninputschema; unknownotherwise: the JSON value for a JSON body, the text for a text or form body, the bytes for other bytes, theFormDataor stream as it was sent, andundefinedwithout 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.SentRequest
A request the fake backend received, as the Recorder keeps it.
Prop
Type