Tests with a real database
Test a booking and its jobs with a transaction that rolls back after each test.
createTestRegistry runs your app’s functions against a real database inside a test transaction.
Set up the test registry
Use the app’s registry and pass test services. Start and roll back a transaction for each test.
import { createTestRegistry } from "logic-sdk/testing";
import { afterEach, beforeEach, expect, test } from "vitest";
const mailer = memoryMailer();
const t = createTestRegistry(registry, {
services: { mailer },
now: "2026-07-01T10:00:00Z",
});
beforeEach(async () => {
mailer.sent.length = 0;
await t.begin();
});
afterEach(() => t.rollback());- Transactions. Each top-level mutation uses a savepoint. A failed mutation rolls back its own writes. Use one test at a time per
t. - Services. Pass each service the test touches. A missing service throws with its name.
- Config. Pass
configto override settings. - Integrations. Use
t.registry.as(scope)when the integration builds the caller. It uses the same transaction.
Run the function and check the result
Arrange data with real functions. Run the mutation as a staff scope.
test("confirming a booking sends one email", async () => {
const booking = await requestBooking(t);
await t.as(staffScope).run(bookings.confirm, { bookingId: booking.id });
expect(mailer.sent).toHaveLength(1);
});Read rows through t.db. Use t.stub(fn, impl) to simulate a function failure.
Inspect waiting jobs
Set jobs: "manual" in createTestRegistry to collect deliveries in t.jobs.queued. The default, "inline", runs due listeners once after each mutation. Both modes use a test adapter. It checks which jobs the app’s adapter can hold or replace.
await t.jobs.drain({ until: "2026-07-09T09:00:00Z" });- Inspect.
t.eventsrecords events even before delivery. Read scheduled calls int.jobs.scheduled. - Drain.
drain()runs jobs due by the test clock and their follow-ups. Withuntil, each waiting job sees its due time inctx.now(). - One listener. Use
t.jobs.perform(tasks.prepareStay, payload, { scope }). Omit the options for a listener with its own scope.performskipswhenand resolves"discarded"if the loader returnsnull. - Failures. A failing listener makes
run,drain, orperformthrow after the mutation commits. A listener failure after at.registrycall surfaces inrollback. - Crons. Tick crons in either mode.
The test adapter cannot start a registry with extra jobs.queues. Use the real adapter to test separate queues.
Use PGlite for in-process Postgres tests. Use a Postgres server for concurrency tests since PGlite runs one connection at a time.