-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcreateProgram.test.ts
101 lines (87 loc) · 2.51 KB
/
createProgram.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { createProgram } from "./createProgram";
import * as _helpers from "./helpers";
import { loadEnvConfig } from "@next/env";
vi.mock("./helpers");
vi.mock("@next/env", () => ({
loadEnvConfig: vi.fn(),
}));
describe("createProgram", () => {
const writeOut = vi.fn();
const writeErr = vi.fn();
const program = createProgram({
writeOut,
writeErr,
})
.exitOverride()
.command("test", "test command");
afterEach(() => {
vi.clearAllMocks();
});
it("should create a program", () => {
expect(program).toBeDefined();
expect(() => program.parse([])).toThrowErrorMatchingInlineSnapshot(
`[CommanderError: (outputHelp)]`
);
});
it("should display help", () => {
expect(() =>
program.parse(["-h"], { from: "user" })
).toThrowErrorMatchingInlineSnapshot(`[CommanderError: (outputHelp)]`);
expect(writeOut.mock.calls).toMatchInlineSnapshot(`
[
[
"INTRO
",
],
[
"Usage: unleash [options] [command]
Options:
-V, --version output the version number
-d, --debug output extra debugging (default: false)
-h, --help display help for command
Commands:
test test command
help [command] display help for command
",
],
]
`);
expect(writeErr.mock.calls).toMatchInlineSnapshot(`[]`);
});
it("should display version", () => {
expect(() =>
program.parse(["-V"], { from: "user" })
).toThrowErrorMatchingInlineSnapshot(`[CommanderError: VERSION]`);
expect(writeOut.mock.calls).toMatchInlineSnapshot(`
[
[
"VERSION
",
],
]
`);
expect(writeErr.mock.calls).toMatchInlineSnapshot(`[]`);
});
it("should have an ability to attach commands", () => {
const action = vi.fn().mockReturnValueOnce("action");
const newProgram = program
.command("testCommand")
.description("description")
.action(action);
newProgram.parse(["testCommand"], { from: "user" });
expect(action).toHaveBeenCalled();
});
test("preSubcommand hook should load environment variables", async () => {
program.command("sub").action(async () => {});
await program.parseAsync(["sub"], { from: "user" });
expect(vi.mocked(loadEnvConfig)).toHaveBeenCalledOnce();
expect(vi.mocked(loadEnvConfig)).toHaveBeenCalledWith(
process.cwd(),
undefined,
{
info: expect.any(Function),
error: expect.any(Function),
}
);
});
});