Open beta

Skills· Official

Testing skill for coding agents

Write tests that prove behaviour, starting from a plain sentence of what must stay true, so they catch real regressions in sign-in, money and data.

When to use it. When you add a feature that must keep working, fix a bug (write the failing test first), or change anything touching sign-in, permissions, money, or data that mustn't be lost.

A test earns its place by failing when behaviour breaks and passing when it doesn't. Work from what must stay true, not from the shape of the code.

Steps

  1. Write the sentence first. One plain statement of behaviour that must hold — "A refund never exceeds what was paid", "Someone who isn't signed in can't read an order". Use it as the test's name.
  2. Pick the layer that actually exercises it. Unit-test pure logic. Integration-test anything that crosses a boundary — a query, an HTTP handler, a permission check — because permission and data bugs live in the wiring, not in the helpers. Choose the lowest layer that genuinely runs the behaviour.
  3. Drive it with inputs; assert on outputs and effects. Call the function, hit the route, run the query — then assert on the response, the stored row, the event that was sent.
  4. Watch it fail first. For a bug, write the test, run it, and see it fail for the right reason before you fix the code. A test you never saw fail may not test anything.
  5. Cover the edges. Empty, one, many; the boundary (off by one, the token that expires exactly now, 30 days against 31); invalid and hostile input; the caller who isn't allowed.
  6. Make it independent and deterministic. Fresh state for every test (a rolled-back transaction, a fresh in-memory database, unique ids). No dependence on order. Control the clock and randomness. No real network calls.
  7. Make failures explain themselves. Assertion messages that say what was expected, what happened, and why it matters.
  8. Run it the way CI will — the whole suite, twice — then keep it.

Test behaviour, not source text

Never assert that code contains something. Such a test passes when the behaviour is broken and fails when someone rewords a comment.

TypeScript
// ✗ breaks on a rename, and passes when the check is bypassedassert.ok(readFileSync("app/api/orders/route.ts", "utf8").includes("requireUser")); // ✓ proves the behaviourconst res = await GET(new Request("https://example.com/api/orders/42")); // no sessionassert.equal(res.status, 401);

Use a real database engine

Query and permission bugs hide behind mocks. Run tests against the real engine with your real migrations. If CI has no database server, use one that runs in-process — PGlite for Postgres, SQLite in memory — and apply the same migrations, so the test exercises the actual schema.

In a Gaitro project

  • Turn each must-stay-true sentence into a check that runs on every change: gaitro check add "<sentence>" --run "<test command>" --keep.
  • Checks run in a fresh sandbox: Node and npm, internet access, about 90 seconds per check. A check that needs Postgres asks for one (--service postgres) and gets an empty database in DATABASE_URL: run your migrations in the check's command. Before relying on a new check, run it there: gaitro check run <handle> --setup. gaitro check env lists what's available.
  • A failing check blocks the ship. Fix the code, not the test — unless the test was wrong, and then say so.

Where tests pay off

Sign-in, authorization (the non-member gets a 403), money, data integrity, and migrations. Skip tests of getters, framework behaviour, and implementation details.

Done when

  • Each must-stay-true sentence has a test named for it
  • Each new test was seen failing before the fix
  • The suite passes on its own and in a full run, twice in a row
  • No test asserts on source text, sleeps to wait, or calls the real network

Report back

The sentences the code now guarantees, the tests that prove them and how to run them, and anything you couldn't cover — with the reason.

Traps

  • Mocking the thing under test until the test proves nothing.
  • Snapshot tests nobody reads, updated whenever they fail.
  • Tests that pass alone and fail together because they share state.
  • Sleeping a fixed time instead of waiting for the actual condition.

More skills