JSPM

  • Created
  • Published
  • Downloads 4185
  • Score
    100M100P100Q114727F
  • License MIT

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.

Terminal Text Parsing Support Checklist Refer to Full List of Ansi Escape Codes that need to be handled.

  • Normal text without ansi escape codes
  • Colored text (Not thouroughly tested)
  • Cursor movement (Basic Support. Not tested)
  • Erase Line/Screen Clear (Basic Support. Not tested)
  • Screen Modes (No Support)
  • Private Modes (No Support)
  • Multiple Arguments (No Support. Difficult to support this)

Installation

With NPM:

npm install --save-dev cli-testing-library 

With Yarn:

yarn add --dev cli-testing-library

Example

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();
  });
});