forked from tj/commander.js
/
command.unknownOption.test.js
98 lines (84 loc) · 2.56 KB
/
command.unknownOption.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
const commander = require('../');
// Checking for detection of unknown options, including regression tests for some past issues.
describe('unknownOption', () => {
// Optional. Use internal knowledge to suppress output to keep test output clean.
let writeErrorSpy;
beforeAll(() => {
writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });
});
afterEach(() => {
writeErrorSpy.mockClear();
});
afterAll(() => {
writeErrorSpy.mockRestore();
});
test('when specify unknown option with subcommand and action handler then error', () => {
const program = new commander.Command();
program
.exitOverride()
.command('info')
.action(() => {});
let caughtErr;
try {
program.parse(['node', 'test', 'info', '--NONSENSE']);
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.unknownOption');
});
test('when specify unknown option with subcommand argument and action handler then error', () => {
const program = new commander.Command();
program
.exitOverride()
.command('info <file>')
.action(() => {});
let caughtErr;
try {
program.parse(['node', 'test', 'info', 'a', '--NONSENSE']);
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.unknownOption');
});
test('when specify unknown option with program and action handler then error', () => {
const program = new commander.Command();
program
.exitOverride()
.arguments('[file]')
.action(() => {});
let caughtErr;
try {
program.parse(['node', 'test', '--NONSENSE']);
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.unknownOption');
});
test('when specify unknown option with program argument and action handler then error', () => {
// Regression test from #965
const program = new commander.Command();
program
.exitOverride()
.arguments('[file]')
.action(() => {});
let caughtErr;
try {
program.parse(['node', 'test', 'info', 'a', '--NONSENSE']);
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.unknownOption');
});
test('when specify unknown option with simple program then error', () => {
const program = new commander.Command();
program
.exitOverride();
let caughtErr;
try {
program.parse(['node', 'test', '--NONSENSE']);
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toBe('commander.unknownOption');
});
});