cli-testlab is a specialized test framework designed for Node.js command-line interface (CLI) applications. Currently at stable version 6.0.1, it provides a streamlined API for executing shell commands, capturing their standard output and error streams, and asserting on their content. Key features include the `execCommand` function, which integrates robust assertion capabilities for positive, negative, and error-based output checks, supporting both string and array inputs for comprehensive validation. It also facilitates the management of environment variables per command execution and offers a `FileTestHelper` class for automatic cleanup of test-generated files. This library differentiates itself by focusing specifically on the unique challenges of CLI testing, offering built-in utilities that abstract away common complexities like child process management and output parsing, making it simpler to write reliable and maintainable tests for CLI tools without relying on heavy general-purpose test runners for these specific tasks.
npm install cli-testlabVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic command execution, output assertions, error handling, and file management with automatic cleanup using `FileTestHelper` within a typical test suite.
Consult the official cli-testlab v6 changelog on GitHub or npm for specific migration instructions and updated API signatures.
Always prepend `execCommand` calls with `await` within `async` test functions: `await execCommand(...)`.
Ensure your testing environment uses Node.js v18 or newer. Update Node.js or configure your project's `engines` field in `package.json` to reflect this requirement.
Use `expectedOutput` or `notExpectedOutput` for commands expected to succeed (exit code 0). Use `expectedErrorMessage` for commands expected to fail (non-zero exit code).
Always instantiate `FileTestHelper` in a `beforeEach` hook and ensure `fileHelper.cleanup()` is called in an `afterEach` hook for reliable test isolation and resource management.
Ensure your project is configured for ESM, typically by setting `"type": "module"` in `package.json`, and use `import { execCommand } from 'cli-testlab'`. If using a bundler like Webpack or Rollup, ensure its configuration properly handles ESM modules.If the command is expected to fail, add `expectedErrorMessage: 'Partial or full error message'` to the `execCommand` options. If it's *not* expected to fail, investigate the CLI command and its environment (`baseDir`, `env`) to understand why it's returning a non-zero exit code.
Verify the exact output of your CLI command (e.g., by running it manually or inspecting `result.stdout` in debug) and adjust the `expectedOutput` string to precisely match. Remember that assertions are case-sensitive and whitespace-sensitive.
Ensure the command string is correct and includes the necessary prefix (e.g., `node` for Node.js scripts). Check the `baseDir` option to confirm the working directory for the command execution is correct and the target script/executable exists at that relative path. If it's a globally installed binary, ensure your test runner environment has the correct PATH configured.
No dependency data recorded yet.