/
command.showHelpAfterError.test.js
122 lines (110 loc) · 3.72 KB
/
command.showHelpAfterError.test.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const commander = require('../');
describe('showHelpAfterError with message', () => {
const customHelpMessage = 'See --help';
function makeProgram() {
const writeMock = jest.fn();
const program = new commander.Command();
program
.exitOverride()
.showHelpAfterError(customHelpMessage)
.configureOutput({ writeErr: writeMock });
return { program, writeMock };
}
test('when missing command-argument then shows help', () => {
const { program, writeMock } = makeProgram();
program.argument('<file>');
let caughtErr;
try {
program.parse([], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.missingArgument');
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);
});
test('when missing option-argument then shows help', () => {
const { program, writeMock } = makeProgram();
program.option('--output <file>');
let caughtErr;
try {
program.parse(['--output'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.optionMissingArgument');
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);
});
test('when missing mandatory option then shows help', () => {
const { program, writeMock } = makeProgram();
program.requiredOption('--password <cipher>');
let caughtErr;
try {
program.parse([], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.missingMandatoryOptionValue');
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);
});
test('when unknown option then shows help', () => {
const { program, writeMock } = makeProgram();
let caughtErr;
try {
program.parse(['--unknown-option'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.unknownOption');
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);
});
test('when too many command-arguments then shows help', () => {
const { program, writeMock } = makeProgram();
program
.allowExcessArguments(false);
let caughtErr;
try {
program.parse(['surprise'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.excessArguments');
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);
});
test('when unknown command then shows help', () => {
const { program, writeMock } = makeProgram();
program.command('sub1');
let caughtErr;
try {
program.parse(['sub2'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.unknownCommand');
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);
});
test('when invalid option choice then shows help', () => {
const { program, writeMock } = makeProgram();
program.addOption(new commander.Option('--color').choices(['red', 'blue']));
let caughtErr;
try {
program.parse(['--color', 'pink'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.invalidArgument');
expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);
});
});
test('when showHelpAfterError() and error and then shows full help', () => {
const writeMock = jest.fn();
const program = new commander.Command();
program
.exitOverride()
.showHelpAfterError()
.configureOutput({ writeErr: writeMock });
try {
program.parse(['--unknown-option'], { from: 'user' });
} catch (err) {
}
expect(writeMock).toHaveBeenLastCalledWith(program.helpInformation());
});