command-line-test is a focused Node.js utility library designed for performing integration tests against command-line interfaces and executables. Currently at version 1.0.10, it provides a unified API to execute shell commands and processes using Node.js's `child_process` module capabilities, including `fork`, `spawn`, `exec`, and `execFile`. The library simplifies the capture and analysis of standard output (`stdout`), standard error (`stderr`), and exit codes from executed commands, making it ideal for verifying CLI tool behavior. It supports asynchronous operations via Promises, traditional Node.js callbacks, and generator-based `yield` (often used with frameworks like `co` or `co-mocha`). Its primary differentiator is its straightforward approach to command execution and result parsing within a test environment, offering a stable and mature solution for CLI automation testing. The project appears to be in a maintenance phase, with infrequent updates since its stable 1.x release series.
npm install command-line-testVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `command-line-test` to execute a shell command and process its `stdout` and `stderr` using Promises. It includes an example of a successful command and one designed to fail.
Always use `const CliTest = require('command-line-test');` when importing the library.Prefer the Promise-based API (`cliTest.exec().then(...)` or `await cliTest.exec(...)`) for modern Node.js projects to avoid potential compatibility issues with generator runtimes. If using `yield`, ensure your test environment is configured to transpile or run generators.
For complex arguments, consider passing an array of arguments to `cliTest.spawn` or `cliTest.execFile` instead of a single command string to `cliTest.exec`, which leverages Node.js's safer argument handling. Alternatively, ensure proper shell quoting/escaping for your specific shell.
Verify the command name is correct and that it is installed and accessible via your system's PATH environment variable. For local binaries, provide the full path or ensure your `node_modules/.bin` is in the PATH if using `npm run` scripts.
Either use `async/await` for asynchronous operations, configure your test setup with Babel to transpile generators, or ensure a `co`-compatible test runner is used. The Promise-based API is generally recommended for modern JavaScript.
Ensure you are using `const CliTest = require('command-line-test');` to import the module, as it is a CommonJS package that exports the `CliTest` class directly.No dependency data recorded yet.