Coffee is a Node.js library designed for robust and fluent testing of command-line interfaces (CLIs). It abstracts the complexities of `child_process.fork` and `child_process.spawn`, providing a streamlined API for executing and asserting against CLI outputs and exit codes. The library, currently stable at version 5.5.1, offers powerful assertion chains for standard output (stdout), standard error (stderr), and process exit codes, supporting both exact string matches and regular expressions. Coffee differentiates itself with features like a debug mode for printing live stdio, interaction capabilities for prompts, and an extensible architecture for creating custom assertion rules. It ships with TypeScript type definitions, making it well-suited for modern JavaScript and TypeScript development workflows, often integrated into test frameworks like Mocha or Jest.
npm install coffeeVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `coffee.fork` to test a Node.js CLI script with arguments, asserting its stdout, stderr, and exit code, and `coffee.spawn` for general shell commands.
Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json` or using `.mjs` files) and update `require('coffee')` statements to `import coffee from 'coffee'`. For custom extensions, use `import { Coffee, Rule } from 'coffee'`.Always include `\n` at the end of expected `stdout` or `stderr` strings if the child process typically outputs a newline. Alternatively, use regular expressions (e.g., `/your output\n?$/`) for more flexible matching.
Ensure every `coffee` chain (after `fork` or `spawn` and any `expect` calls) explicitly ends with `.end()` and `await` it. For example: `await coffee.fork(...).expect(...).end();`
Review the CLI script to understand why it's exiting with an error code. If the non-zero code is expected for an error condition, update `expect('code', 0)` to `expect('code', 1)` (or the appropriate error code).Carefully compare the expected string with the actual output. Ensure newline characters (`\n`) are correctly included or excluded. Consider using regular expressions for more robust matching if exact string comparison is too brittle (e.g., `expect('stdout', /^Hello World\n?$/)`).Ensure `.end()` is called at the end of your `coffee` assertion chain, and that you `await` the resulting promise. Also, verify that the `coffee` object is correctly initialized and not `null` or `undefined`.
Verify that the command (`<command>`) is correctly spelled and installed on the system where tests are run. Ensure its directory is included in the system's PATH environment variable. For Node.js scripts, consider using `coffee.fork()` instead.
No dependency data recorded yet.