A readerly command prints something different for each reader, so one test per reader catches a change that only an agent or only CI would see. You don't need a real terminal or real environment variables: createOut takes an Io with the environment, the three streams and a clock, and reads nothing else from process.
readerly tests itself this way in test/golden.test.ts. The examples below use the same acme check command, from test/golden/check.ts, and ran against those golden files.
Make the command testable
Section titled "Make the command testable"Give the command a second parameter for io and detection, and spread it into createOut. In production you call check(argv) and both fall back to the real process:
import { createOut, type OutOptions } from "readerly";
export async function check(
argv: string[],
options: Pick<OutOptions, "io" | "detection"> = {},
): Promise<number> {
const out = createOut<{ files: number; durationMs: number }>({
tool: "acme",
command: "check",
version: "1.2.0",
argv,
...options,
summary: (result) =>
result.files === undefined
? undefined
: `in ${result.files} files (${((result.durationMs ?? 0) / 1000).toFixed(1)}s)`,
});
// The scan, the confirm and the next line, as in test/golden/check.ts.
return out.end();
}A fake Io
Section titled "A fake Io"Io has five fields. Each one decides part of what the command prints:
| Field | What it controls |
|---|---|
env | Detection, color and <TOOL>_FORMAT. A partial io with env replaces process.env completely, so the test sees only the variables you list. |
stdout | Where the answer goes. isTTY decides terminal or pipe. |
stderr | Prompts, progress, annotations and --verbose. A prompt needs isTTY: true. |
stdin | Where prompt answers come from. A prompt needs isTTY: true. |
now | The clock for progress. A clock stuck at 0 keeps progress silent, since a step under 2 seconds prints nothing. |
This helper keeps everything the command writes. On a TTY it answers each prompt with the next string from answers, and ends stdin when it runs out, which makes the prompt take its default:
import { readFileSync } from "node:fs";
import { PassThrough } from "node:stream";
import type { Detection, Env, Io } from "readerly";
import { describe, expect, it } from "vitest";
import { check } from "../src/check.ts";
const golden = (name: string) =>
readFileSync(new URL(`./golden/${name}`, import.meta.url), "utf8");
/** Streams that keep what the command writes. On a TTY, each prompt gets the next answer. */
function fakeIo(env: Env, tty: boolean, answers: string[] = []) {
let stdout = "";
let stderr = "";
const stdin = Object.assign(new PassThrough(), { isTTY: tty });
const io: Io = {
env,
stdout: { isTTY: tty, columns: 120, write: (chunk: string) => (stdout += chunk) },
stderr: {
isTTY: tty,
write(chunk: string) {
stderr += chunk;
if (!chunk.startsWith("? ")) return;
const answer = answers.shift();
setImmediate(() => (answer === undefined ? stdin.end() : stdin.write(`${answer}\n`)));
},
},
stdin,
now: () => 0,
};
return { io, stdout: () => stdout, stderr: () => stderr };
}Prompts start with ? on stderr, and readerly reads the answer from io.stdin with node:readline. setImmediate writes the answer after the prompt is on screen, as a person would.
One test per reader
Section titled "One test per reader"The same file goes on with one test for each reader. Each test sets only the variables that reader would have:
describe("acme check", () => {
it("prints findings, then asks, at a terminal", async () => {
const run = fakeIo({ TERM: "xterm-256color", NO_COLOR: "1" }, true, ["n"]);
expect(await check([], { io: run.io })).toBe(1);
expect(run.stdout()).toBe(golden("terminal.stdout.txt"));
expect(run.stderr()).toBe("? Apply 2 safe fixes? (y/N) ");
});
it("gives a coding agent the compact report", async () => {
const run = fakeIo({ AI_AGENT: "claude-code" }, false);
expect(await check([], { io: run.io })).toBe(1);
expect(run.stdout()).toBe(golden("agent.stdout.txt"));
expect(run.stderr()).toBe("");
});
it("annotates files on GitHub Actions", async () => {
const run = fakeIo({ CI: "true", GITHUB_ACTIONS: "true" }, false);
expect(await check([], { io: run.io })).toBe(1);
expect(run.stdout()).toBe(golden("actions.stdout.txt"));
expect(run.stderr()).toBe(golden("actions.stderr.txt"));
});
it("prints one JSON document with --format json", async () => {
const run = fakeIo({}, false);
expect(await check(["--format", "json"], { io: run.io })).toBe(1);
expect(JSON.parse(run.stdout())).toMatchObject({
schema: "acme.check/v1",
status: "failed",
summary: { error: 2, warn: 1, info: 0 },
});
});
});NO_COLOR: "1" keeps escape codes out of the terminal golden. To test color, set FORCE_COLOR: "1" instead and keep a second golden.
The agent's golden, agent.stdout.txt, holds:
acme check: failed. 2 errors, 1 warning in 212 files (0.8s).
error ACME001 src/app.ts:14:3 Route handler reads process.env at module scope. fix: Read it inside the handler
error ACME004 src/db.ts:3:1 Top-level await in a server entry. fix: Move the call into the plugin setup
warn ACME010 vite.config.ts:8:5 Alias shadows a workspace package
next: acme check --fix (applies 2 safe fixes)
next: acme explain <code> (explains one code)Golden files
Section titled "Golden files"Keep one file per reader and stream, named after both: terminal.stdout.txt, agent.stdout.txt, actions.stderr.txt, json.stdout.json. Compare them with toBe, never with a loose match, so a changed space or glyph fails the test. Write each file once from a run you read line by line, then commit it. readerly's own goldens are in test/golden/.
To test the order of writes, such as findings before a prompt, keep every chunk in an array tagged with its stream instead of two strings. test/golden.test.ts checks that the ACME010 row comes before the ? prompt and the next: line after it.
Skip detection
Section titled "Skip detection"detection replaces detect(), so the environment no longer picks the reader. It still decides <TOOL>_FORMAT and color. Use it to test one reader without building its environment:
it("takes a fixed detection instead of reading env", async () => {
const pipe: Detection = {
reader: "pipe",
reason: "test",
stdout: { isTTY: false },
stderr: { isTTY: false },
stdin: { isTTY: false },
};
const run = fakeIo({ AI_AGENT: "claude-code" }, false);
expect(await check([], { io: run.io, detection: pipe })).toBe(1);
expect(run.stdout()).toContain("next: acme check --fix (applies 2 safe fixes)");
});AI_AGENT is set, but the fixed detection says pipe, so the command prints text.
For a table of environments and flags with the reader and profile each one gets, see test/reader-matrix.test.ts.