-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathoptions.optional.test.js
70 lines (63 loc) · 2.5 KB
/
options.optional.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('../');
// option with optional value, no default
describe('option with optional value, no default', () => {
test('when option not specified then value is undefined', () => {
const program = new commander.Command();
program
.option('--cheese [type]', 'cheese type');
program.parse(['node', 'test']);
expect(program.opts().cheese).toBeUndefined();
});
test('when option specified then value is as specified', () => {
const program = new commander.Command();
program
.option('--cheese [type]', 'cheese type');
const cheeseType = 'blue';
program.parse(['node', 'test', '--cheese', cheeseType]);
expect(program.opts().cheese).toBe(cheeseType);
});
test('when option specified without value then value is true', () => {
const program = new commander.Command();
program
.option('--cheese [type]', 'cheese type');
program.parse(['node', 'test', '--cheese']);
expect(program.opts().cheese).toBe(true);
});
test('when option specified without value and following option then value is true', () => {
// optional options do not eat values with dashes
const program = new commander.Command();
program
.option('--cheese [type]', 'cheese type')
.option('--some-option');
program.parse(['node', 'test', '--cheese', '--some-option']);
expect(program.opts().cheese).toBe(true);
});
});
// option with optional value, with default
describe('option with optional value, with default', () => {
test('when option not specified then value is default', () => {
const defaultValue = 'default';
const program = new commander.Command();
program
.option('--cheese [type]', 'cheese type', defaultValue);
program.parse(['node', 'test']);
expect(program.opts().cheese).toBe(defaultValue);
});
test('when option specified then value is as specified', () => {
const defaultValue = 'default';
const program = new commander.Command();
program
.option('--cheese [type]', 'cheese type', defaultValue);
const cheeseType = 'blue';
program.parse(['node', 'test', '--cheese', cheeseType]);
expect(program.opts().cheese).toBe(cheeseType);
});
test('when option specified without value then value is default', () => {
const defaultValue = 'default';
const program = new commander.Command();
program
.option('--cheese [type]', 'cheese type', defaultValue);
program.parse(['node', 'test', '--cheese']);
expect(program.opts().cheese).toBe(defaultValue);
});
});