Skip to content

Commit

Permalink
test: update test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Dec 13, 2020
1 parent 813269a commit f101991
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
15 changes: 15 additions & 0 deletions test/fixtures/cli-exit-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const enquirer = require("enquirer");

enquirer
.prompt({
type: "input",
name: "text",
message: "Please input some text",
})
.then(({ text }) => {
if (!text) {
console.error("Invalid input");
process.exit(1);
}
console.log(text);
});
34 changes: 31 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
"use strict";

const path = require("path");

const runTest = require("../");

const DOWN = "\x1B\x5B\x42";
const ENTER = "\x0D";
const SPACE = "\x20";

const cliPath = `${__dirname}/fixtures/cli.js`;
const fixturesPath = path.join(__dirname, "fixtures");
const cliPath = path.join(fixturesPath, "cli.js");
const cliExitCodePath = path.join(fixturesPath, "cli-exit-code.js");

describe("cli-prompts-test", () => {
it("picks a single option", async () => {
const { stdout, exitCode } = await runTest(
const { exitCode, stdout } = await runTest(
[cliPath],
[`${DOWN}${DOWN}${SPACE}${ENTER}`]
);

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

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

// Assertions
expect(exitCode).toBe(0);
expect(stdout).toContain("You chose aqua, fuchsia, green");
});

it("returns an exit code of 0", async () => {
const { exitCode, stderr, stdout } = await runTest(
[cliExitCodePath],
["sample text", ENTER]
);

// Assertions
expect(exitCode).toBe(0);
expect(stdout).toContain("sample text");
expect(stderr).toBeFalsy();
});

it("returns an exit code of 1", async () => {
const { exitCode, stderr } = await runTest([cliExitCodePath], [ENTER]);

// Assertions
expect(exitCode).toBe(1);
expect(stderr).toContain("Invalid input");
});
});

0 comments on commit f101991

Please sign in to comment.