Skip to Content
Senri

Testing

Call an app without a server with testClient() and request() from @senrijs/senri/testing

app.test.ts
import { ,  } from "@senrijs/senri/testing";

// call app.fetch with a typed client, without a server
const  = <>(, { : true });
await ("POST /login");
const  = await ("GET /me");          // sent with the login cookie
(.).(200);

// routes outside the table, or requests the typed client cannot send, use request() (it returns a Response)
const  = await (, "POST /upload", { :  });
(.).(404);

It works with any test runner (bun test, node --test, Vitest, deno test).

testClient() and request()

FunctionUse it forReturns
testClient<Api>(app, options)Calling table routes, typedThe same result as the typed client
request(app, "METHOD /path", init)Routes outside the table, or requests the typed client cannot sendA Response

Options

  • Client address: passed as 127.0.0.1, so getIp(), ipRestriction() and the development mode's localhost check work as with a real server. Change it with { ip }
  • Cloudflare Workers apps: pass { env, ctx } (for c.env and c.executionCtx). Then, as on Workers, no client address is passed
  • Cookies: with { cookies: true }, Set-Cookie from a response is sent with the next requests (one per name; domain and path are not checked)
  • Headers: { headers } sets headers sent with every request

Prop

Type

On this page