Skip to Content
Testing

Check what was sent

Assert on the requests your code sent, and learn how they're matched.

Sometimes what matters in a test isn't what comes back but what your code sent: that a delete went to the right URL, or that a form carried the right fields. The fake backend records every request it receives, so you can assert on them. This page shows how to read those records, and how the fake backend picks a handler for each request.

Read the recorded requests

FetchTest.requests gives you the requests the fake backend received, oldest first:

import * as  from "effect/Effect";
import * as  from "effect/Layer";

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

class  extends .<>()("Api", {
  : "https://api.test",
}) {}

const  = .("DELETE /todos/:id");

const  = .([
  .(, () => .),
]);

export const  = .(function* () {
  const  = yield* ;
  yield* .()({ : { : 1 } });
  const [] = yield* .;
  // sent.method: "DELETE", sent.url: "https://api.test/todos/1",
  // sent.route: "DELETE /todos/:id", sent.params: { id: "1" }
  return ;
}).(.(..(.())));

The records live in FetchTest.Recorder, a service that the fake backend's layer provides, with a new, empty record each time the layer is built. Notice Layer.provideMerge in the last line. Under a client's layer, Layer.provide would hide the recorder from your test, while Layer.provideMerge keeps it available.

Each recorded request has the following properties:

  • method, such as "DELETE";
  • url, with its query string;
  • route and params, the route and the path params it matched, as strings;
  • query, the raw query parameters;
  • headers, with lowercase names;
  • body, read back: the value of a JSON body, the text of a text or form body, the bytes of other bytes, a FormData or stream as it was sent, or undefined without one.

A request that no route matched is recorded too, with route set to undefined and params set to {}.

How requests are matched

The fake backend picks a handler for each request by these rules:

  • Method and path. A route matches a request with the same method, whose path ends with the route's path. So GET /todos/:id answers https://api.test/v1/todos/1, whatever the client's baseUrl. To match the whole URL instead, pass the baseUrl to the layer: FetchTest.layer(handlers, { baseUrl: "https://api.test/v1" }).

  • The root route, GET /, matches the path / alone, since every path with a trailing slash ends with it. Under a baseUrl with a path, pass that baseUrl to the layer.

  • The best match wins. When several routes match, the one with the most segments wins, then the one with the most literal segments, so GET /todos/count wins over GET /todos/:id. After that, the one listed first wins.

  • Absolute routes, such as POST https://uploads.example.com/files, match that URL only.

  • No match is a defect, whose message lists the backend's routes, and the ones that match the path with another method:

    FetchTest: no handler for POST https://api.test/todos/1.
    GET /todos/:id matches its path, but not its method.
    The handlers are:
      - GET /todos/:id

What's next

On this page