Package Exports
- cli-testing-library
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cli-testing-library) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
[WIP] CLI Testing Library
A testing library that allows you to test input and outputs of your CLI command.
Note: This is a work in progress. The API is likely going to change.
Installation
npm install --save cli-testing-library # yarn add cli-testing-libraryExample
Test file
// experiment.test.js
const { createCommandInterface } = require('cli-testing-library');
test('should print appropriate greetings', async () => {
const commandInterface = createCommandInterface('node ./experiment.js', {
cwd: __dirname
});
await commandInterface.type('Saurabh\n');
await commandInterface.type('22\n');
const terminal = await commandInterface.getOutput();
expect(terminal.textAfter("What's your name?")).toBe('Hi, Saurabh!');
expect(terminal.textAfter("What's your age?")).toBe('So you are 22!');
});Code that is being tested A node program that asks user for their name and age, and then prints a greeting.
// experiment.js
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question(`What's your name?`, (name) => {
console.log(`Hi, ${name}!`);
readline.question(`What's your age?`, (age) => {
console.log(`So you are ${age}!`);
readline.close();
});
});