Testing CLI Commands
Test a CLI by invoking its action with argv and a mock stdout, assert the exit code and output, and avoid spawning a process for every case.
On this page
Introduction
This guide covers testing CLI commands, a recurring concern in the cli-tooling scenario. Testing a CLI by spawning a process is slow and brittle, and it tests the shell as much as the command. Invoking the action function with argv and a mock stdout, then asserting the exit code and output, is faster, deterministic, and covers the branches that matter.
Export the action
Keep the action function separate from the Commander wiring so a test can call it directly with options, which is what makes the CLI unit-testable.
The trade-off worth naming is that keep the action function separate from the commander wiring so a test can call it directly with options, which is what makes the cli unit-testable adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
export function buildAction(opts) {
if (!opts.src) { console.error('src required'); return 1; }
console.log('built ' + opts.src);
return 0;
}
Invoke with argv
Call the action with a parsed options object and a mock stdout, so the test is deterministic and fast and does not spawn a process.
The trade-off worth naming is that call the action with a parsed options object and a mock stdout, so the test is deterministic and fast and does not spawn a process adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
import { buildAction } from './cli.js';
const logs = [];
const orig = console.log; console.log = (m) => logs.push(m);
const code = buildAction({ src: 'a.ts' });
console.log = orig;
assert(code === 0 && logs[0] === 'built a.ts');
Assert exit and output
Assert the return code and the captured output, so the test pins both the side effect and the status the user sees.
The trade-off worth naming is that assert the return code and the captured output, so the test pins both the side effect and the status the user sees adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Error paths
Test the missing-arg and failure paths without spawning, so a bad input returns the right code and message, which is where CLIs usually regress.
The trade-off worth naming is that test the missing-arg and failure paths without spawning, so a bad input returns the right code and message, which is where clis usually regress adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Smoke test the binary
Add a few spawned smoke tests for the binary and argv wiring, so the unit tests cover logic and the smoke tests cover the shell layer.
The trade-off worth naming is that add a few spawned smoke tests for the binary and argv wiring, so the unit tests cover logic and the smoke tests cover the shell layer adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test help
Invoke help and assert it lists each command and option, so a missing description fails the test rather than shipping a blank help line.
The trade-off worth naming is that invoke help and assert it lists each command and option, so a missing description fails the test rather than shipping a blank help line adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
FAQ
- Spawn or invoke?
- Invoke the action for unit tests, since it is fast and deterministic; spawn a process for a few smoke tests to confirm the binary and argv wiring, which is the best of both.
- How do I capture stdout?
- Pass a mock writable stream or spy on console.log, so the test reads the output without a real process and without polluting the test runner output.
Report a problem with this articleOpens a pre-filled issue on GitHub.