You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

cli-testing-library

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cli-testing-library

A testing library that allows you to test input and outputs of your CLI command.

0.0.11
Source
npmnpm
Version published
Weekly downloads
2.7K
-8.6%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 05 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts