1. cli-prompts-test
Write e2e tests for CLI apps with ease
cli-prompts-test
Package: cli-prompts-test
Created by: jamesgeorge007
Last modified: Tue, 12 Apr 2022 06:41:01 GMT
Version: 0.3.0
License: GPL-3.0
Downloads: 203
Repository: https://github.com/jamesgeorge007/cli-prompts-test

Install

npm install cli-prompts-test
yarn add cli-prompts-test

CLI Prompts Test

Write e2e tests for CLI apps with ease.

Installation

 $ npm install --save-dev cli-prompts-test

API

runTest(args, answers, options?)

  • args: CLI args to pass in.
  • answers: answers to be passed to stdout (simulate user input).
  • options: Optionally specify the testPath (defaults to process.cwd()) and timeout (defaults to 500ms) between keystrokes.

Usage

 // cli.js

const enquirer = require("enquirer");

const choices = ['First option', 'Second option', 'Third option'];

enquirer
  .prompt({
    type: "select",
    name: "option",
    message: "Choose from below",
    choices,
  })
  .then(({ option }) => {
    console.log(`You chose ${option}`);
  });
 // test.js

const runTest, { DOWN, ENTER } = require("cli-prompts-test");

const cliPath = `${__dirname}/cli.js`;

describe("cli-prompts-test", () => {
  it("picks first option", async () => {
    const { exitCode, stdout } = await runTest(
      [cliPath],
      [ENTER]
    );

    // Assertions
    expect(exitCode).toBe(0);
    expect(stdout).toContain("You chose First option");
  });

  it("picks second option", async () => {
    const { exitCode, stdout } = await runTest(
      [cliPath],
      [`${DOWN}${ENTER}`]
    );

    // Assertions
    expect(exitCode).toBe(0);
    expect(stdout).toContain("You chose Second option");
  });

  it("picks third option", async () => {
    const { exitCode, stdout } = await runTest(
      [cliPath],
      [`${DOWN}${DOWN}${ENTER}`]
    );

    // Assertions
    expect(exitCode).toBe(0);
    expect(stdout).toContain("You chose Third option");
  });
});

Find an example here.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code