Skip to Content
Effetch Query

Testing

Test queries, mutations and components with a fresh client.

Your tests can run every query against a fake backend, with no network and no React. Your queries run against whatever HttpClient your client's layer gets, so in tests, give it FetchTest.layer from @monarcode/effetch/test: a fake backend made from your endpoints, with params, body and the response typed by each endpoint. QueryClient.layer gives an Effect program a client, disposed when the program ends:

The examples import api.ts and todos.ts from Getting started.

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

import { ,  } from "@monarcode/effetch-query";
import {  } from "@monarcode/effetch/test";

import {  } from "./api.js";
import { , ,  } from "./todos.js";

const  = .([
  .(, ({  }) =>
    // params are strings, as they are in the URL
    .({
      : (.),
      : "Write tests",
      : false,
    }),
  ),
  .(, 503),
]);

const  = ..(.());

export const  = .(({ : { : 1 } })).(
  .(.({ :  })),
);

Notice that the fake is provided under the client's own layer, as here. A class that overrides layer to provide its own HttpClient ignores one given from outside.

React tests. Render under a QueryProvider with a fresh client, and dispose it after each test.

Time. To control staleTime, refetchInterval and gcTime, fake only the timers. Leave setImmediate real, since Effect's scheduler runs on it:

import {  } from "vitest";

.({ : ["setTimeout", "clearTimeout", "Date"] });