forked from tj/commander.js
/
command.unknownOption.test.js
85 lines (72 loc) · 2.21 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
const commander = require('../');
// Checking for detection of unknown options, including regression tests for some past issues.
describe('.version', () => {
// Optional. Use internal knowledge to suppress output to keep test output clean.
let consoleErrorSpy;
beforeAll(() => {
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
});
afterEach(() => {
consoleErrorSpy.mockClear();
});
afterAll(() => {
consoleErrorSpy.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', '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');
});
});