forked from tj/commander.js
/
command.allowUnknownOptions.test.js
70 lines (57 loc) · 1.77 KB
/
command.allowUnknownOptions.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
const commander = require('../');
// Not testing output, just testing whether an error is detected.
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 program option then error', () => {
const program = new commander.Command();
program
.exitOverride()
.option('-p, --pepper', 'add pepper');
expect(() => {
program.parse(['node', 'test', '-m']);
}).toThrow();
});
test('when specify unknown program option and allowUnknownOption then no error', () => {
const program = new commander.Command();
program
.exitOverride()
.allowUnknownOption()
.option('-p, --pepper', 'add pepper');
expect(() => {
program.parse(['node', 'test', '-m']);
}).not.toThrow();
});
test('when specify unknown command option then error', () => {
const program = new commander.Command();
program
.exitOverride()
.command('sub')
.option('-p, --pepper', 'add pepper')
.action(() => { });
expect(() => {
program.parse(['node', 'test', 'sub', '-m']);
}).toThrow();
});
test('when specify unknown command option and allowUnknownOption then no error', () => {
const program = new commander.Command();
program
.exitOverride()
.command('sub')
.allowUnknownOption()
.option('-p, --pepper', 'add pepper')
.action(() => { });
expect(() => {
program.parse(['node', 'test', 'sub', '-m']);
}).not.toThrow();
});
});