Skip to content

Commit 32c64fb

Browse files
feat: add exit code for prompt test (#1)
* feat: add exitCode * test: add exit code 0 * docs: update usage info * test: update test suite Co-authored-by: jamesgeorge007 <[email protected]>
1 parent f0ad381 commit 32c64fb

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,38 @@ const cliPath = `${__dirname}/cli.js`;
4646

4747
describe("cli-prompts-test", () => {
4848
it("picks first option", async () => {
49-
const { stdout } = await runTest(
49+
const { exitCode, stdout } = await runTest(
5050
[cliPath],
5151
[ENTER]
5252
);
53+
54+
// Assertions
55+
expect(exitCode).toBe(0);
5356
expect(stdout).toContain("You chose First option");
5457
});
5558

5659
it("picks second option", async () => {
57-
const { stdout } = await runTest(
60+
const { exitCode, stdout } = await runTest(
5861
[cliPath],
5962
[`${DOWN}${ENTER}`]
6063
);
64+
65+
// Assertions
66+
expect(exitCode).toBe(0);
6167
expect(stdout).toContain("You chose Second option");
6268
});
6369

6470
it("picks third option", async () => {
65-
const { stdout } = await runTest(
71+
const { exitCode, stdout } = await runTest(
6672
[cliPath],
6773
[`${DOWN}${DOWN}${ENTER}`]
6874
);
75+
76+
// Assertions
77+
expect(exitCode).toBe(0);
6978
expect(stdout).toContain("You chose Third option");
7079
});
7180
});
7281
```
7382

74-
Find an example [here](https://github.com/madlabsinc/mevn-cli/blob/develop/jest/helpers.js).
83+
Find an example [here](https://github.com/madlabsinc/mevn-cli/blob/develop/jest/helpers.js).

index.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,22 @@ module.exports = (args, answers, options) => {
3737
return new Promise((resolve) => {
3838
const obj = {};
3939

40-
// Booleans to keep track of completion status
41-
let stdoutCompleted = false;
42-
let stderrCompleted = false;
43-
44-
const resolveWithObj = () => {
45-
if (stdoutCompleted && stderrCompleted) {
46-
resolve(obj);
47-
}
48-
};
49-
5040
runner.stdout.pipe(
5141
concat((result) => {
52-
stdoutCompleted = true;
5342
obj.stdout = result.toString();
54-
resolveWithObj();
5543
})
5644
);
5745

5846
runner.stderr.pipe(
5947
concat((result) => {
60-
stderrCompleted = true;
6148
obj.stderr = result.toString();
62-
resolveWithObj();
6349
})
6450
);
51+
52+
runner.on("exit", (exitCode) => {
53+
obj.exitCode = exitCode;
54+
resolve(obj);
55+
});
6556
});
6657
};
6758

test/fixtures/cli-exit-code.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const enquirer = require("enquirer");
2+
3+
enquirer
4+
.prompt({
5+
type: "input",
6+
name: "text",
7+
message: "Please input some text",
8+
})
9+
.then(({ text }) => {
10+
if (!text) {
11+
console.error("Invalid input");
12+
process.exit(1);
13+
}
14+
console.log(text);
15+
});

test/test.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,57 @@
11
"use strict";
22

3+
const path = require("path");
4+
35
const runTest = require("../");
46

57
const DOWN = "\x1B\x5B\x42";
68
const ENTER = "\x0D";
79
const SPACE = "\x20";
810

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

1115
describe("cli-prompts-test", () => {
1216
it("picks a single option", async () => {
13-
const { stdout } = await runTest(
17+
const { exitCode, stdout } = await runTest(
1418
[cliPath],
1519
[`${DOWN}${DOWN}${SPACE}${ENTER}`]
1620
);
21+
22+
// Assertions
23+
expect(exitCode).toBe(0);
1724
expect(stdout).toContain("You chose blue");
1825
});
1926

2027
it("picks multiple options", async () => {
21-
const { stdout } = await runTest(
28+
const { exitCode, stdout } = await runTest(
2229
[cliPath],
2330
[`${SPACE}${DOWN}${DOWN}${DOWN}${SPACE}${ENTER}${DOWN}${SPACE}${ENTER}`]
2431
);
32+
33+
// Assertions
34+
expect(exitCode).toBe(0);
2535
expect(stdout).toContain("You chose aqua, fuchsia, green");
2636
});
37+
38+
it("returns an exit code of 0", async () => {
39+
const { exitCode, stderr, stdout } = await runTest(
40+
[cliExitCodePath],
41+
["sample text", ENTER]
42+
);
43+
44+
// Assertions
45+
expect(exitCode).toBe(0);
46+
expect(stdout).toContain("sample text");
47+
expect(stderr).toBeFalsy();
48+
});
49+
50+
it("returns an exit code of 1", async () => {
51+
const { exitCode, stderr } = await runTest([cliExitCodePath], [ENTER]);
52+
53+
// Assertions
54+
expect(exitCode).toBe(1);
55+
expect(stderr).toContain("Invalid input");
56+
});
2757
});

0 commit comments

Comments
 (0)