Skip to content

Test over HTTP ​

Start your app on port 0 to get a free local port. Make requests with fetch and close the server when the test finishes. No custom request adapter needed.

ts
import assert from "node:assert/strict";
import { once } from "node:events";
import type { TestContext } from "node:test";
import type { App } from "@portablesheep/brevity";

export async function serve(t: TestContext, app: App): Promise<string> {
  const server = app.listen(0, "127.0.0.1");
  t.after(async () => {
    server.closeAllConnections();
    await new Promise<void>((resolve, reject) => {
      server.close((error) => (error ? reject(error) : resolve()));
    });
  });
  await once(server, "listening");
  const address = server.address();
  assert(address && typeof address !== "string");
  return `http://127.0.0.1:${address.port}`;
}

Register cleanup before awaiting listening, then read the assigned port from server.address(). once(server, "listening") also rejects on a server error.

Run the examples ​

sh
npm run docs:check

The test suite covers JSON routes, form errors, escaping, persistence after reopening SQLite, HEAD, decorators, and Supabase SDK wiring. Use a separate temporary database for each test and close sockets before closing storage.

closeAllConnections() is useful for test cleanup, not graceful production shutdown. For browser behavior, use Playwright.

Small core. Build your way.