CLI Testing Library provides a set of simple and complete utilities for testing command-line interface (CLI) applications, encouraging good testing practices by focusing on user interaction and observable output rather than internal implementation details. Inspired by the popular React Testing Library, it helps developers simulate user input and assert on console output (stdout, stderr). The library is currently at version 3.0.1, released in January 2025, and maintains an active development cadence with regular updates and major version bumps that introduce breaking changes and improvements. Its key differentiators include a focus on accessibility and user experience in tests, first-class TypeScript support, and integration with popular test runners like Jest and Vitest through dedicated extensions. It abstracts away the complexities of spawning and managing child processes, making CLI testing straightforward.
npm install cli-testing-libraryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to render a Node.js CLI, simulate user interactions (arrow keys, enter), and assert on the console output using custom Jest matchers like `toBeInTheConsole`.
Update your imports to `import 'cli-testing-library/jest'` for Jest or `import 'cli-testing-library/vitest'` for Vitest. Ensure this import is executed before your tests.
If you were directly accessing elements in `stdout` or `stderr`, adjust your code to access `item.contents` instead of `item` directly. For example, `output.stdout[0].contents`.
Review any code that might have indirectly relied on `chalk`'s API or behavior. Most users will not be directly impacted, but if issues arise, consider migrating to `picocolors` directly if styling is critical.
Consider running your CLI tests on Linux-based CI environments. If a fix is found, contribute to the tracking issue on GitHub (https://github.com/crutchcorn/cli-testing-library/issues/13).
For Jest, add `import 'cli-testing-library/jest'` to your test setup file or at the top of relevant test files. For Vitest, use `import 'cli-testing-library/vitest'`.
Update your import statement to use the new test runner-specific paths: `import 'cli-testing-library/jest'` for Jest, or `import 'cli-testing-library/vitest'` for Vitest.
Either upgrade `cli-testing-library` to v2.0.0 or newer to match the expected object shape, or adjust your code to access the raw string/buffer directly (e.g., `output.stdout[0]`) if remaining on an older version.