-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
command.action.test.js
139 lines (121 loc) · 4.6 KB
/
command.action.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
const commander = require('../');
// Test some behaviours of .action not covered in more specific tests.
test('when .action called then command passed to action', () => {
const actionMock = jest.fn();
const program = new commander.Command();
const cmd = program.command('info').action(actionMock);
program.parse(['node', 'test', 'info']);
expect(actionMock).toHaveBeenCalledWith(cmd.opts(), cmd);
});
test('when .action called then this is set to command', () => {
const program = new commander.Command();
let actionThis;
const cmd = program.command('info').action(function () {
actionThis = this;
});
program.parse(['node', 'test', 'info']);
expect(actionThis).toBe(cmd);
});
test('when .action called then program.args only contains args', () => {
// At one time program.args was being modified to contain the same args as the call to .action
// and so included the command as an extra and unexpected complex item in array.
const program = new commander.Command();
program.command('info <file>').action(() => {});
program.parse(['node', 'test', 'info', 'my-file']);
expect(program.args).toEqual(['info', 'my-file']);
});
test.each(getTestCases('<file>'))(
'when .action on program with required argument via %s and argument supplied then action called',
(methodName, program) => {
const actionMock = jest.fn();
program.action(actionMock);
program.parse(['node', 'test', 'my-file']);
expect(actionMock).toHaveBeenCalledWith('my-file', program.opts(), program);
},
);
test.each(getTestCases('<file>'))(
'when .action on program with required argument via %s and argument not supplied then action not called',
(methodName, program) => {
const actionMock = jest.fn();
program
.exitOverride()
.configureOutput({ writeErr: () => {} })
.action(actionMock);
expect(() => {
program.parse(['node', 'test']);
}).toThrow();
expect(actionMock).not.toHaveBeenCalled();
},
);
// Changes made in #729 to call program action handler
test('when .action on program and no arguments then action called', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program.action(actionMock);
program.parse(['node', 'test']);
expect(actionMock).toHaveBeenCalledWith(program.opts(), program);
});
test.each(getTestCases('[file]'))(
'when .action on program with optional argument via %s supplied then action called',
(methodName, program) => {
const actionMock = jest.fn();
program.action(actionMock);
program.parse(['node', 'test', 'my-file']);
expect(actionMock).toHaveBeenCalledWith('my-file', program.opts(), program);
},
);
test.each(getTestCases('[file]'))(
'when .action on program without optional argument supplied then action called',
(methodName, program) => {
const actionMock = jest.fn();
program.action(actionMock);
program.parse(['node', 'test']);
expect(actionMock).toHaveBeenCalledWith(undefined, program.opts(), program);
},
);
test.each(getTestCases('[file]'))(
'when .action on program with optional argument via %s and subcommand and program argument then program action called',
(methodName, program) => {
const actionMock = jest.fn();
program.action(actionMock);
program.command('subcommand');
program.parse(['node', 'test', 'a']);
expect(actionMock).toHaveBeenCalledWith('a', program.opts(), program);
},
);
// Changes made in #1062 to allow this case
test.each(getTestCases('[file]'))(
'when .action on program with optional argument via %s and subcommand and no program argument then program action called',
(methodName, program) => {
const actionMock = jest.fn();
program.action(actionMock);
program.command('subcommand');
program.parse(['node', 'test']);
expect(actionMock).toHaveBeenCalledWith(undefined, program.opts(), program);
},
);
test('when action is async then can await parseAsync', async () => {
let asyncFinished = false;
async function delay() {
await new Promise((resolve) => setTimeout(resolve, 100));
asyncFinished = true;
}
const program = new commander.Command();
program.action(delay);
const later = program.parseAsync(['node', 'test']);
expect(asyncFinished).toBe(false);
await later;
expect(asyncFinished).toBe(true);
});
function getTestCases(arg) {
const withArguments = new commander.Command().arguments(arg);
const withArgument = new commander.Command().argument(arg);
const withAddArgument = new commander.Command().addArgument(
new commander.Argument(arg),
);
return [
['.arguments', withArguments],
['.argument', withArgument],
['.addArgument', withAddArgument],
];
}