Registry / testing / command-line-test

command-line-test

JSON →
library1.0.10jsnpmunverified

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-test
INSTALL
IMPORT
SIG · COMMAND-LINE-TEST
C
command-line-test
testingjavascriptv1.0.10
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CliTest
const CliTest = require('command-line-test');
This package is a CommonJS module. Use `require` for direct import in Node.js environments.
CliTest
const CliTest = require('command-line-test').CliTest;
import CliTest from 'command-line-test';
While the primary export is the class directly, some might expect a named export or a default export for ESM. This is incorrect as it's a CJS module exporting a class.
CliTest
const CliTest = require('command-line-test');
import { CliTest } from 'command-line-test';
Attempting to import `CliTest` as a named export from an ES Module will fail because the package is a CommonJS module and does not provide native named exports for ESM consumers.

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.

const CliTest = require('command-line-test'); const assert = require('assert'); async function runCliTestExample() { const cliTest = new CliTest(); console.log('--- Testing `cat package.json` ---'); try { const res = await cliTest.exec('cat package.json'); // res object contains error, stdout, stderr const pkg = JSON.parse(res.stdout); console.log(`Package name: ${pkg.name}`); assert.strictEqual(pkg.name, 'command-line-test', 'Expected package name to match'); console.log('Test successful: Successfully read and parsed package.json.'); } catch (error) { console.error('Test failed:', error.message, error.stderr); } console.log('\n--- Testing a non-existent command ---'); try { const res = await cliTest.exec('nonexistent-command'); // This should ideally throw or return an error in 'res.error' console.log('Unexpected success for nonexistent command. Output:', res); } catch (error) { console.log('Test caught expected error for nonexistent command.'); assert.ok(error.message.includes('command not found') || error.stderr.includes('command not found'), 'Expected 'ENOENT' or 'command not found' error'); } } // A minimal assert implementation if not using a test runner // or for demonstrating simple assertions within the quickstart. // In a real project, use a proper assertion library like 'should' or 'chai'. // For demonstration, a basic `assert` is good enough. runCliTestExample();
Debug
Known issues
gotchaThis package is a CommonJS module (`require` syntax) and does not provide native ES Module (`import` syntax) exports. Attempting to use `import` directly may lead to errors or require specific build tool configurations.
fix
Always use `const CliTest = require('command-line-test');` when importing the library.
affects: >=1.0.0
gotchaThe documentation and examples show usage with generator functions (using the `yield` keyword). While this works with `co`-style runners (e.g., `co-mocha`), modern Node.js development primarily uses `async/await` for asynchronous control flow. Ensure your test runner and environment support generators if you choose this pattern, otherwise use the Promise-based API.
fix
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.
affects: >=1.0.0
gotchaWhen running shell commands, pay close attention to shell escaping for arguments, especially when dealing with paths containing spaces or special characters. Incorrect escaping can lead to command interpretation errors or security vulnerabilities.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
Error: spawn <command> ENOENT
The command specified in `cliTest.exec` or `cliTest.spawn` was not found in the system's PATH.
fix
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.
ReferenceError: regeneratorRuntime is not defined
You are using `yield` in a test function without a transpiler (like Babel) or a runtime (like `co`) that provides the `regeneratorRuntime` for generator functions.
fix
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.
TypeError: CliTest is not a constructor
The `command-line-test` module was imported incorrectly, leading to `CliTest` not being the expected class constructor.
fix
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.
Upgrade
Version history
1.0.10latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Google (search)
1
OpenAI (training)
1
Resources
command-line-test — npm install command-line-test · libregistry