テスト
@senrijs/senri/testing の testClient()・request() で、サーバーを立てずにアプリを呼ぶ
import { , } from "@senrijs/senri/testing";
// 型付きクライアントで、サーバーを立てずに app.fetch を呼ぶ
const = <>(, { : true });
await ("POST /login");
const = await ("GET /me"); // ログインの Cookie が付く
(.).(200);
// 表にないルートや、型付きでは送れないリクエストは request() で(Response が返る)
const = await (, "POST /upload", { : });
(.).(404);どのテストランナー(bun test・node --test・Vitest・deno test)でも使えます。
testClient() と request()
| 関数 | 使うとき | 返すもの |
|---|---|---|
testClient<Api>(app, options) | 表のルートを、型付きで呼ぶ | 型付きクライアントと同じ結果 |
request(app, "METHOD /path", init) | 表にないルート、型付きでは送れないリクエスト | Response |
オプション
- 接続元:
127.0.0.1として渡します。getIp()・ipRestriction()・開発モードの localhost の判定が、本物のサーバーと同じに動きます。{ ip }で変えられます - Cloudflare Workers のアプリ:
{ env, ctx }を渡します(c.env・c.executionCtx)。このときは Workers と同じく、接続元は渡しません - Cookie:
{ cookies: true }で、前の応答のSet-Cookieを、次のリクエストに付けます(名前ごとに 1 つ。domain と path は見ません) - ヘッダー:
{ headers }で、毎回付けるヘッダーを決められます
名前
型