Write e2e tests for CLI apps with ease.
$ npm install --save-dev cli-prompts-test
args
: CLI args to pass in.answers
: answers to be passed tostdout
(simulate user input).options
: Optionally specify thetestPath
(defaults toprocess.cwd()
) andtimeout
(defaults to500ms
) between keystrokes.
// 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
import runTest, { DOWN, ENTER } from "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.