Guides
Writing Tests
Best practices for organizing and writing Repterm tests
Test Structure
Repterm uses a familiar describe / test structure. Import the test utilities from repterm and organize your tests into logical groups.
import { test, describe, expect } from 'repterm';
describe('basic', () => {
test('success command', async ({ $ }) => {
const result = await $`echo "Hello"`;
await expect(result).toSucceed();
await expect(result).toContainInOutput('Hello');
});
test('failure command', async ({ $ }) => {
const result = await $`cat /definitely-not-exists`;
await expect(result).toFail();
await expect(result).toHaveStderr('No such file');
});
});Running Commands with $
The $ tagged template literal is the primary way to run commands. It provides automatic shell escaping for interpolated values, protecting against injection issues.
test('escaped variables', async ({ $ }) => {
const filename = 'my file with spaces.txt';
const result = await $`ls ${filename}`;
// filename is properly escaped in the shell command
});Multi-Step Tests
Use test.step to break complex tests into named steps. Each step is reported individually, making failures easier to diagnose.
test('multi-step workflow', async ({ $ }) => {
await test.step('create file', async () => {
await $`echo "content" > /tmp/test-file.txt`;
});
await test.step('verify file', async () => {
const result = await $`cat /tmp/test-file.txt`;
await expect(result).toContainInOutput('content');
});
await test.step('cleanup', async () => {
await $`rm /tmp/test-file.txt`;
});
});Recording-Ready Tests
Mark tests with { record: true } so they can produce terminal recordings when run with the --record flag.
test('demo', { record: true }, async ({ $ }) => {
await $`pwd`;
});See the Recording Mode guide for full details.
Tips and Best Practices
- Use
$tagged templates for commands. The automatic shell escaping prevents common pitfalls with special characters and spaces. - Use
describeto group related tests. This keeps your test output organized and makes it easier to run subsets of tests. - Use
test.stepfor multi-step tests. Named steps provide better error reporting and make complex test flows readable. - Use
{ record: true }to mark recordable tests. Only tests with this flag will produce.castfiles when running in recording mode. - Use
silent: truewhen you need reliable exit codes in PTY mode. PTY mode may report exit code-1for some commands;silent: trueavoids this. - Prefer output assertions over exit code assertions in PTY mode. Checking
toContainInOutputortoHaveStderris more reliable than checking exit codes under PTY. - Test files should use
.tsor.jsextensions. Repterm discovers test files by these extensions automatically. - Use
setup.tsorsetup.jsfor shared setup. A setup file placed in a directory is auto-loaded before that directory's tests run.