Testing

For exact factory, request, result, and disposal signatures, see the primate/test API reference.

In-process requests

Use primate/test to exercise an application's request pipeline without binding a network port. Pass a URL and the same optional initialization used by the standard WHATWG Request constructor:

import test from "primate/test";

await using app = await test(import.meta.dirname);

const response = await app.request(
  "https://example.test/api/items",
  {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ name: "example" }),
  },
);

if (response.status !== 200) throw new Error(await response.text());

request() accepts a URL string or URL and the standard optional RequestInit object.

The harness builds an isolated testing application and uses the same Request-to-Response boundary as the served HTTP callback. It runs normal application initialization and serve hooks, asset and session setup, handle and route hooks, matching, request bags, body decoding and schemas, response conversion, abort handling, and error mapping. It validates configured migrations but never applies them.

Call close() or use await using to dispose the harness and remove its isolated build output. Each harness has its own bundled application state, and concurrent requests retain separate request context. Application hooks can still have external side effects; configure test databases and other dependencies through the application's normal supported configuration.

In-process tests cover the application pipeline, not the HTTP transport. Use a served application fixture when testing WebSocket upgrades, TLS, transport timeouts, connection behavior, streaming backpressure, browser behavior, or other network boundaries. WebSocket upgrades are unavailable without a served application.

Previous
Building
Next
Intro