-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathoptions.opts.test.js
74 lines (66 loc) · 2.74 KB
/
options.opts.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
const commander = require('../');
// Test the `.opts()` way of accessing option values.
// Basic coverage of the main option types (leaving out negatable flags and options with optional values).
test('when .version used with storeOptionsAsProperties() then version in opts', () => {
const program = new commander.Command();
const version = '0.0.1';
program
.storeOptionsAsProperties()
.version(version);
program.parse(['node', 'test']);
expect(program.opts()).toEqual({ version });
});
test('when .version used with storeOptionsAsProperties(false) then version not in opts', () => {
// New behaviour, stop storing version as an option value.
const program = new commander.Command();
const version = '0.0.1';
program
.storeOptionsAsProperties(false)
.version(version);
program.parse(['node', 'test']);
expect(program.opts()).toEqual({ });
});
describe.each([true, false])('storeOptionsAsProperties is %s', (storeOptionsAsProperties) => {
test('when boolean flag not specified then not in opts', () => {
const program = new commander.Command();
program.storeOptionsAsProperties(storeOptionsAsProperties);
program
.option('--pepper', 'add pepper');
program.parse(['node', 'test']);
expect(program.opts()).toEqual({ });
});
test('when boolean flag specified then value true', () => {
const program = new commander.Command();
program.storeOptionsAsProperties(storeOptionsAsProperties);
program
.option('--pepper', 'add pepper');
program.parse(['node', 'test', '--pepper']);
expect(program.opts()).toEqual({ pepper: true });
});
test('when option with required value not specified then not in opts', () => {
const program = new commander.Command();
program.storeOptionsAsProperties(storeOptionsAsProperties);
program
.option('--pepper <flavour>', 'add pepper');
program.parse(['node', 'test']);
expect(program.opts()).toEqual({ });
});
test('when option with required value specified then value as specified', () => {
const pepperValue = 'red';
const program = new commander.Command();
program.storeOptionsAsProperties(storeOptionsAsProperties);
program
.option('--pepper <flavour>', 'add pepper');
program.parse(['node', 'test', '--pepper', pepperValue]);
expect(program.opts()).toEqual({ pepper: pepperValue });
});
test('when option with default value not specified then default value in opts', () => {
const pepperDefault = 'red';
const program = new commander.Command();
program.storeOptionsAsProperties(storeOptionsAsProperties);
program
.option('--pepper <flavour>', 'add pepper', pepperDefault);
program.parse(['node', 'test']);
expect(program.opts()).toEqual({ pepper: pepperDefault });
});
});