Skip to Content
Testing

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:

  • params has the route's params, as strings.
  • body is decoded with the endpoint's input schema, so body.due is a Date here. A multipart body is read back from the form, with its files as Files.
  • query is 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 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 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

On this page