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.1.0
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
  • 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

Examples

Check out Interactive Examples on Stackblitz

Eg 1: Hello World Example

// hello-world.test.js
const { createCommandInterface } = require('cli-testing-library');

test('should print greetings', async () => {
  const commandInterface = createCommandInterface('node ./hello-world.js', {
    cwd: __dirname, // Directory from where you want to run the command 
  });
  await commandInterface.type('Saurabh\n');
  const terminal = await commandInterface.getOutput();
  expect(terminal.stringOutput).toBe("What's your name?Hi, Saurabh!");
});

The code that we're testing-

// hello-world.js
const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout,
});

readline.question(`What's your name?`, (name) => {
  console.log(`Hi, ${name}!`);
  readline.close();
});

Eg 2: Tokenized Output

Check out this example of StackBlitz

Sometimes you may want to test if the output has correct color and graphics. You can use the .tokenizedOutput method to get tokens in the output.

Check out list of tokens that library outputs.

// colored-output.test.js
const { createCommandInterface } = require('cli-testing-library');

test('should have bold red text', async () => {
  const commandInterface = createCommandInterface('node ./colored-output.js', {
    cwd: __dirname,
  });
  const terminal = await commandInterface.getOutput();
  expect(terminal.tokenizedOutput).toBe("This has a [BOLD_START][RED_START]red and bold[COLOR_END][BOLD_END] text.");
});

More Examples on Stackblitz

Big Shoutout to

FAQs

Package last updated on 30 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