Developer

Testing

Unit-test methods against an in-memory context with createTestCtx.

The SDK ships a test harness that runs a method against an in-memory context: real entity storage backed by in-memory SQLite, fake clock and uuid, captured events, and a fake cron scheduler.

createTestCtx

import { createTestCtx } from "@rightplace/applet-sdk/testing";
import { Feed } from "../entities.js";
import { addFeed } from "../methods/index.js";
import { feedAdded } from "../events.js";

const ctx = await createTestCtx({
  entities: [Feed],
  events: [feedAdded],
  scopes: ["rss:write"],
  now: () => 1700000000000,
  uuid: () => "00000000-0000-0000-0000-000000000001",
});

const feed = await addFeed.run({ url: "https://example.com/rss" }, ctx);
expect(feed.url).toBe("https://example.com/rss");

expect(ctx.emittedEvents).toContainEqual({
  name: "rss.feedAdded",
  payload: { feed: expect.objectContaining({ url: "https://example.com/rss" }) },
});

Fake cron

await ctx.cron.fake.tick("rss.refreshAll");
expect(ctx.cron.fake.registered).toContain("rss.refreshAll");

What the harness covers

  • In-memory SQLite via better-sqlite3, a dev dependency that never ships.
  • Captured events on ctx.emittedEvents.
  • Deterministic now and uuid so assertions are stable.

Methods with runIn: "backend" that call native commands need the integration harness rather than this unit harness, because the native command is not present in a unit test.