terrably
Development

Testing

Unit testing resource logic with node:test.

Unit testing

Resource lifecycle methods (create, read, update, delete) are plain async functions. Test them directly — no gRPC server needed.

tests/server.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { MyCloudServer } from "../src/resources/server.js";
import { Diagnostics } from "terrably";
import type { CreateContext, ReadContext } from "terrably";

// Minimal mock provider
const mockProvider = { apiBase: "https://api.mycloud.example" } as any;

test("create returns state with id", async () => {
  const resource = new MyCloudServer(mockProvider);
  const ctx: CreateContext = {
    diagnostics: new Diagnostics(),
    typeName: "mycloud_server",
  };

  const result = await resource.create(ctx, { name: "test", region: "us-east-1" });

  assert.ok(result["id"], "should have an id");
  assert.equal(result["name"], "test");
  assert.ok(!ctx.diagnostics.hasErrors());
});

test("read returns null for 404", async () => {
  const resource = new MyCloudServer(mockProvider);
  const ctx: ReadContext = {
    diagnostics: new Diagnostics(),
    typeName: "mycloud_server",
  };

  const result = await resource.read(ctx, { id: "nonexistent-id" });

  assert.strictEqual(result, null);
});

Running tests

# Build first so TypeScript compiles
pnpm exec tsc

node --test dist/tests/*.test.js

Or via the package script –

pnpm run test

Testing with a real local API

Start your API server before running tests –

node dist/api-server/index.js &
API_PID=$!

node --test dist/tests/server.test.js

kill $API_PID

Last updated on

On this page