-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcommand.error.test.js
62 lines (50 loc) · 1.63 KB
/
command.error.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
const commander = require('../');
test('when error called with message then message displayed on stderr', () => {
const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});
const stderrSpy = jest
.spyOn(process.stderr, 'write')
.mockImplementation(() => {});
const program = new commander.Command();
const message = 'Goodbye';
program.error(message);
expect(stderrSpy).toHaveBeenCalledWith(`${message}\n`);
stderrSpy.mockRestore();
exitSpy.mockRestore();
});
test('when error called with no exitCode then process.exit(1)', () => {
const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});
const program = new commander.Command();
program.configureOutput({
writeErr: () => {},
});
program.error('Goodbye');
expect(exitSpy).toHaveBeenCalledWith(1);
exitSpy.mockRestore();
});
test('when error called with exitCode 2 then process.exit(2)', () => {
const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});
const program = new commander.Command();
program.configureOutput({
writeErr: () => {},
});
program.error('Goodbye', { exitCode: 2 });
expect(exitSpy).toHaveBeenCalledWith(2);
exitSpy.mockRestore();
});
test('when error called with code and exitOverride then throws with code', () => {
const program = new commander.Command();
let errorThrown;
program
.exitOverride((err) => {
errorThrown = err;
throw err;
})
.configureOutput({
writeErr: () => {},
});
const code = 'commander.test';
expect(() => {
program.error('Goodbye', { code });
}).toThrow();
expect(errorThrown.code).toEqual(code);
});