Plain scripts and test files
A file run by testingterm run can be either:
- A plain script: top level code that throws to fail. Good for one scenario.
- A test file: several
test()cases, each with its own terminals.
import { test, expect } from "testingterm";
test("shows help", async ({ launch }) => {
const term = launch(["mycli", "--help"]);
await expect(term).toHaveText("Usage:");
await expect(term).toHaveExited({ code: 0 });
});
test("rejects unknown commands", async ({ launch }) => {
const term = launch("mycli frobnicate");
await expect(term).toHaveExited({ code: 2 });
await expect(term).toHaveText(/unknown command/i, { scrollback: true });
});Terminals started with the launch passed to a test close when the test ends, whether it passed or not.
Launching programs
launch(command, options) returns a Terminal right away; you don't need to await it.
- A string runs through
sh -c, so pipes,&&and variables work:launch("ls | wc -l"). - An array runs the program directly with exact arguments:
launch(["node", "app.mjs", "--flag"]). - No command runs your login shell.
Common options:
launch(["node", "app.mjs"], {
cols: 120,
rows: 40,
cwd: "./fixtures/project",
env: { NO_COLOR: undefined, APP_ENV: "test" },
isolate: true,
theme: "light",
timeout: 10_000,
});env values of undefined remove the variable. isolate: true gives the program an empty HOME (and XDG folders) so local config files can't change the result. See the library API for every option.
Running files
$ testingterm run tests/*.test.mjs| Option | What it does |
|---|---|
--video on, --video retain-on-failure |
Record every terminal, or keep videos only for failures |
--retries 2 |
Run failed tests again before reporting them |
--test-timeout 60000 |
Per test timeout (default 30s) |
--timeout 10000 |
Default timeout for waits and assertions (default 5s) |
--workers 4 |
How many files run at once (default: CPU count) |
--grep "help" |
Only tests whose name matches |
--out test-results |
Where artifacts go (default a fresh temp folder) |
--json |
Machine readable results |
TypeScript files work too: Node strips the types when it loads them.
Reading results
✓ tests/cli.test.mjs › shows help (258ms)
✗ tests/tui.test.mjs › clicking a task toggles it (5104ms)
expected the terminal to show "1 of 3 done" within 5000ms
screen:
tasks 0 of 3 done
...
artifacts:
test-results/tui.test/.../node-xh64-failure.png
test-results/tui.test/.../node-xh64-failure.txt
1 passed, 1 failed in 5.4sWhen a test fails, testingterm saves a screenshot and the full scrollback of the terminal that failed, and prints the screen in the error. Exit code is 1 when anything failed.
Per test options
test("slow import", async ({ launch }) => { /* ... */ }, { timeout: 60_000, retries: 1 });
test.only("focus on this one", async () => {});
test.skip("not yet", async () => {});The test context also has name and artifacts (the folder for this test's files).