Minimal Test Runner Command Builder
The `test-cmd` package is a minimalist Node.js utility designed to facilitate the creation and execution of simple command-line test runners. Released with its latest stable version 1.7.0 over eight years ago, this package is built entirely on CommonJS modules and leverages older dependencies like `optimist` for argument parsing, `tape` for test assertions, and `through` for stream manipulation. Its primary function is to wrap shell commands and integrate them into a testing flow, making it suitable for black-box testing of command-line interfaces. Due to its age and lack of recent maintenance, it does not support modern JavaScript features like ESM or `async/await` and is generally considered superseded by contemporary testing frameworks. It has an infrequent release cadence, with its last update being many years ago, indicating it is no longer actively maintained.
Common errors
-
ReferenceError: require is not defined
cause Attempting to `require('test-cmd')` in an ECMAScript Module (ESM) context.fixChange your file to use CommonJS (e.g., by ensuring your `package.json` does not have `"type": "module"`, or changing your file extension to `.cjs`), or use a transpiler like Babel if you must use ESM syntax while consuming CJS modules. -
TypeError: createCommand is not a function
cause Incorrectly trying to destructure a named export, or accessing a property that doesn't exist. `test-cmd` exports a single function as its `module.exports`.fixUse `const createCommand = require('test-cmd');` to get the default function export. -
Error: listen EADDRINUSE: address already in use :::<port>
cause While not directly caused by `test-cmd`, old testing patterns often involve spawning multiple processes or servers without proper cleanup. `test-cmd` itself can spawn child processes for testing, and if these don't exit cleanly, they might leave resources like open ports in use.fixEnsure that any child processes or servers spawned by your tests (managed by `test-cmd` or otherwise) are properly terminated or cleaned up after each test run, typically in `t.end()` or `afterEach` hooks.
Warnings
- breaking The package is CommonJS-only. Using `import` statements in an ESM module context will result in a `SyntaxError: Cannot use import statement outside a module` or `require is not defined` error. It requires a Node.js environment configured for CommonJS.
- gotcha The package is unmaintained, with its last update over 8 years ago. It relies on deprecated dependencies like `optimist` for argument parsing, which may have security vulnerabilities or unexpected behavior with newer Node.js versions or complex CLI arguments.
- gotcha Direct integration with modern `async/await` patterns is challenging due to its callback-based API. While it can be wrapped in Promises, the core design predates widespread async/await adoption in Node.js.
Install
-
npm install test-cmd -
yarn add test-cmd -
pnpm add test-cmd
Imports
- createCommand
import createCommand from 'test-cmd';
const createCommand = require('test-cmd'); - testCmd
const { command } = require('test-cmd');const testCmd = require('test-cmd'); testCmd.command(args, fn);
Quickstart
const createCommand = require('test-cmd');
const tape = require('tape');
// Simulate a simple command that echoes arguments
const mockCommand = (args) => {
return `echo 'Hello, ${args.join(' ')}'`;
};
// Define a test using tape and test-cmd
tape('test-cmd basic usage', (t) => {
t.plan(1); // Expect one assertion
const myTestCmd = createCommand();
myTestCmd.command(['world'], (run) => {
// In a real scenario, 'run' would execute a shell command.
// Here, we simulate it.
const simulatedOutput = mockCommand(run.argv);
// 'run' object would typically contain child process stdout/stderr
// For this mock, we just check the generated command string.
t.equal(simulatedOutput, "echo 'Hello, world'", 'should generate the correct echo command');
t.end();
});
});
// To run this: save as e.g., `my-test.js`, then `node my-test.js`
// This quickstart mocks the actual command execution for simplicity,
// but `test-cmd` would typically spawn a child process.