Skip to content

Writing and running tests

tui-test plugs into your language’s existing test runner rather than introducing a custom runner. It provides terminal session helpers, retrying locators, and structured failure artifacts.

Use the test helper for your language so each test gets an isolated session name and closes the child process even when an assertion fails.

Save as terminal.test.mjs and run with node --test terminal.test.mjs:

import test from "node:test";
import { withTerminal } from "@microsoft/tui-test/test";
test("prints Ready and exits cleanly", async () => {
await withTerminal(
{
cols: 80,
rows: 24,
program: [process.execPath, "-e", "console.log('Ready')"],
},
async (terminal) => {
await terminal.getByText("Ready").expect();
await terminal.waitExit();
await terminal.expectExitCode(0);
},
);
});
Terminal window
node --test --test-name-pattern="prints Ready" terminal.test.mjs

Terminal sessions are keyed by name inside each process (or inside the CLI daemon). Sharing a session name across concurrent tests causes those tests to write into the same PTY and close each other’s child process.

  • Use withTerminal (JavaScript), terminal(...) (Python), tuitesttest.New (Go), or TuiTest.ephemeral() so each test generates a unique session name.
  • In Rust, pass a distinct name per test to Session::new, or construct an unshared Session instance per test.
  • Avoid calling process-wide closeAll() / close_all() while parallel tests are still running.
  • Canceling a Python asyncio.Task or dropping a JavaScript promise does not abort an in-flight native operation. Always close sessions in finally blocks, context managers, or t.Cleanup, or call interrupt() first if a wait is blocked.

Terminal apps reflow text based on grid size, so pin cols, rows, backend, cwd, and relevant env variables at launch, especially when comparing snapshots or screenshots.

Synchronize on observable state rather than wall-clock delays. Fixed sleeps run slowly on developer machines and time out on busy CI runners. Wait for visible text (locator.expect()), the disappearance of spinners (expect({ not: true })), shell command completion (waitCommand), or process termination (waitExit). Reserve waitIdle for apps without explicit completion markers, since idle only indicates that output paused temporarily.

When a shell echoes the command string or a layout repeats button labels like Save or Delete, scope your locator with relativeTo, scope, styles, or .unique() instead of relying on screen order. Finally, verify exit codes separately from screen text: seeing "Done" on screen does not guarantee exit code 0, so pair waitCommand or waitExit with expectExitCode(0) when process status matters.

tui-test runs headlessly in Linux, macOS, and Windows CI environments without an X11, Wayland, or desktop session. Enable on-failure traces so CI uploads an interactive HTML report, Markdown summary, and asciinema recording whenever a test fails.

import { setTerminalDefaults } from "@microsoft/tui-test/test";
setTerminalDefaults({
trace: { mode: "on-failure", directory: "artifacts/traces" },
artifacts: {
dir: "artifacts/failures",
onFailure: "all",
includeRecording: true,
},
});

Upload artifacts/ as a workflow artifact on failure in your CI configuration. MP4 recordings additionally require ffmpeg on PATH; see recordings and traces and diagnostics.