Testing
Call an app without a server with testClient() and request() from @senrijs/senri/testing
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()
| Function | Use it for | Returns |
|---|---|---|
testClient<Api>(app, options) | Calling table routes, typed | The same result as the typed client |
request(app, "METHOD /path", init) | Routes outside the table, or requests the typed client cannot send | A Response |
Options
- Client address: passed as
127.0.0.1, sogetIp(),ipRestriction()and the development mode's localhost check work as with a real server. Change it with{ ip } - Cloudflare Workers apps: pass
{ env, ctx }(forc.envandc.executionCtx). Then, as on Workers, no client address is passed - Cookies: with
{ cookies: true },Set-Cookiefrom 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