forked from tj/commander.js
/
options.version.test.js
168 lines (134 loc) · 4.86 KB
/
options.version.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const commander = require('../');
// Test .version. Using exitOverride to check behaviour (instead of mocking process.exit).
describe('.version', () => {
// Optional. Suppress normal output to keep test output clean.
let writeSpy;
beforeAll(() => {
writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
});
afterEach(() => {
writeSpy.mockClear();
});
afterAll(() => {
writeSpy.mockRestore();
});
test('when no .version and specify --version then unknown option error', () => {
const errorMessage = 'unknownOption';
const program = new commander.Command();
// Override unknownOption as convenient way to check fails as expected.
jest.spyOn(program, 'unknownOption').mockImplementation(() => { throw new Error(errorMessage); });
expect(() => {
program.parse(['node', 'test', '--version']);
}).toThrow(errorMessage);
});
test('when no .version then helpInformation does not include version', () => {
const program = new commander.Command();
const helpInformation = program.helpInformation();
expect(helpInformation.includes('Usage')).toBe(true);
expect(helpInformation.includes('version')).toBe(false);
});
test('when specify default short flag then display version', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion);
expect(() => {
program.parse(['node', 'test', '-V']);
}).toThrow(myVersion);
// Test output once as well, rest of tests just check the thrown message.
expect(writeSpy).toHaveBeenCalledWith(`${myVersion}\n`);
});
test('when specify default long flag then display version', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion);
expect(() => {
program.parse(['node', 'test', '--version']);
}).toThrow(myVersion);
});
test('when default .version then helpInformation includes default version help', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.version(myVersion);
const helpInformation = program.helpInformation();
expect(helpInformation.includes('-V, --version')).toBe(true);
expect(helpInformation.includes('output the version number')).toBe(true);
});
test('when specify custom short flag then display version', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion, '-r, --revision');
expect(() => {
program.parse(['node', 'test', '-r']);
}).toThrow(myVersion);
});
test('when specify just custom short flag then display version', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion, '-r');
expect(() => {
program.parse(['node', 'test', '-r']);
}).toThrow(myVersion);
});
test('when specify custom long flag then display version', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion, '-r, --revision');
expect(() => {
program.parse(['node', 'test', '--revision']);
}).toThrow(myVersion);
});
test('when specify just custom long flag then display version', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion, '--revision');
expect(() => {
program.parse(['node', 'test', '--revision']);
}).toThrow(myVersion);
});
test('when custom .version then helpInformation includes custom version help', () => {
const myVersion = '1.2.3';
const myVersionFlags = '-r, --revision';
const myVersionDescription = 'custom description';
const program = new commander.Command();
program
.version(myVersion, myVersionFlags, myVersionDescription);
const helpInformation = program.helpInformation();
expect(helpInformation.includes(myVersionFlags)).toBe(true);
expect(helpInformation.includes(myVersionDescription)).toBe(true);
});
test('when have .version+version and specify version then command called', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program
.version('1.2.3')
.command('version')
.action(actionMock);
program.parse(['node', 'test', 'version']);
expect(actionMock).toHaveBeenCalled();
});
test('when have .version+version and specify --version then version displayed', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion)
.command('version')
.action(() => {});
expect(() => {
program.parse(['node', 'test', '--version']);
}).toThrow(myVersion);
});
});