/
command.hook.test.js
359 lines (333 loc) · 12.1 KB
/
command.hook.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
const commander = require('../');
test('when hook event wrong then throw', () => {
const program = new commander.Command();
expect(() => {
program.hook('silly', () => {});
}).toThrow();
});
test('when no action then action hooks not called', () => {
const hook = jest.fn();
const program = new commander.Command();
program
.hook('preAction', hook)
.hook('postAction', hook);
program.parse([], { from: 'user' });
expect(hook).not.toHaveBeenCalled();
});
describe('action hooks with synchronous hooks, order', () => {
test('when hook preAction then hook called before action', () => {
const calls = [];
const program = new commander.Command();
program
.hook('preAction', () => calls.push('before'))
.action(() => calls.push('action'));
program.parse([], { from: 'user' });
expect(calls).toEqual(['before', 'action']);
});
test('when hook postAction then hook called after action', () => {
const calls = [];
const program = new commander.Command();
program
.hook('postAction', () => calls.push('after'))
.action(() => calls.push('action'));
program.parse([], { from: 'user' });
expect(calls).toEqual(['action', 'after']);
});
test('when hook preAction twice then hooks called FIFO', () => {
const calls = [];
const program = new commander.Command();
program
.hook('preAction', () => calls.push('1'))
.hook('preAction', () => calls.push('2'))
.action(() => calls.push('action'));
program.parse([], { from: 'user' });
expect(calls).toEqual(['1', '2', 'action']);
});
test('when hook postAction twice then hooks called LIFO', () => {
const calls = [];
const program = new commander.Command();
program
.hook('postAction', () => calls.push('1'))
.hook('postAction', () => calls.push('2'))
.action(() => calls.push('action'));
program.parse([], { from: 'user' });
expect(calls).toEqual(['action', '2', '1']);
});
test('when hook preAction at program and sub then hooks called program then sub', () => {
const calls = [];
const program = new commander.Command();
program
.hook('preAction', () => calls.push('program'));
program.command('sub')
.hook('preAction', () => calls.push('sub'))
.action(() => calls.push('action'));
program.parse(['sub'], { from: 'user' });
expect(calls).toEqual(['program', 'sub', 'action']);
});
test('when hook postAction at program and sub then hooks called sub then program', () => {
const calls = [];
const program = new commander.Command();
program
.hook('postAction', () => calls.push('program'));
program.command('sub')
.hook('postAction', () => calls.push('sub'))
.action(() => calls.push('action'));
program.parse(['sub'], { from: 'user' });
expect(calls).toEqual(['action', 'sub', 'program']);
});
test('when hook everything then hooks called nested', () => {
const calls = [];
const program = new commander.Command();
program
.hook('preAction', () => calls.push('pb1'))
.hook('postAction', () => calls.push('pa1'));
program
.hook('preAction', () => calls.push('pb2'))
.hook('postAction', () => calls.push('pa2'));
program.command('sub')
.hook('preAction', () => calls.push('sb'))
.hook('postAction', () => calls.push('sa'))
.action(() => calls.push('action'));
program.parse(['sub'], { from: 'user' });
expect(calls).toEqual(['pb1', 'pb2', 'sb', 'action', 'sa', 'pa2', 'pa1']);
});
});
describe('action hooks context', () => {
test('when hook on program then passed program/program', () => {
const hook = jest.fn();
const program = new commander.Command();
program
.hook('preAction', hook)
.action(() => {});
program.parse([], { from: 'user' });
expect(hook).toHaveBeenCalledWith(program, program);
});
test('when hook on program and call sub then passed program/sub', () => {
const hook = jest.fn();
const program = new commander.Command();
program
.hook('preAction', hook);
const sub = program.command('sub')
.action(() => {});
program.parse(['sub'], { from: 'user' });
expect(hook).toHaveBeenCalledWith(program, sub);
});
test('when hook on sub and call sub then passed sub/sub', () => {
const hook = jest.fn();
const program = new commander.Command();
const sub = program.command('sub')
.hook('preAction', hook)
.action(() => {});
program.parse(['sub'], { from: 'user' });
expect(hook).toHaveBeenCalledWith(sub, sub);
});
test('when hook program on preAction then thisCommand has options set', () => {
expect.assertions(1);
const program = new commander.Command();
program
.option('--debug')
.hook('preAction', (thisCommand) => {
expect(thisCommand.opts().debug).toEqual(true);
})
.action(() => {});
program.parse(['--debug'], { from: 'user' });
});
test('when hook program on preAction and call sub then thisCommand has program options set', () => {
expect.assertions(1);
const program = new commander.Command();
program
.option('--debug')
.hook('preAction', (thisCommand) => {
expect(thisCommand.opts().debug).toEqual(true);
});
program.command('sub')
.action(() => {});
program.parse(['sub', '--debug'], { from: 'user' });
});
test('when hook program on preAction and call sub then actionCommand has sub options set', () => {
expect.assertions(1);
const program = new commander.Command();
program
.hook('preAction', (thisCommand, actionCommand) => {
expect(actionCommand.opts().debug).toEqual(true);
});
program.command('sub')
.option('--debug')
.action(() => {});
program.parse(['sub', '--debug'], { from: 'user' });
});
test('when hook program on preAction then actionCommand has args set', () => {
expect.assertions(1);
const program = new commander.Command();
program
.argument('[arg]')
.hook('preAction', (thisCommand, actionCommand) => {
expect(actionCommand.args).toEqual(['value']);
})
.action(() => {});
program.parse(['value'], { from: 'user' });
});
test('when hook program on preAction then actionCommand has args set with options removed', () => {
expect.assertions(1);
const program = new commander.Command();
program
.argument('[arg]')
.option('--debug')
.hook('preAction', (thisCommand, actionCommand) => {
expect(actionCommand.args).toEqual(['value']);
})
.action(() => {});
program.parse(['value', '--debug'], { from: 'user' });
});
test('when hook program on preAction and call sub then thisCommand has program args set', () => {
expect.assertions(1);
const program = new commander.Command();
program
.argument('[arg]')
.hook('preAction', (thisCommand) => {
expect(thisCommand.args).toEqual(['sub', 'value']);
});
program.command('sub')
.action(() => {});
program.parse(['sub', 'value'], { from: 'user' });
});
test('when hook program on preAction and call sub then actionCommand has sub args set', () => {
expect.assertions(1);
const program = new commander.Command();
program
.hook('preAction', (thisCommand, actionCommand) => {
expect(actionCommand.args).toEqual(['value']);
});
program.command('sub')
.action(() => {});
program.parse(['sub', 'value'], { from: 'user' });
});
});
describe('action hooks async', () => {
test('when async preAction then async from preAction', async() => {
const calls = [];
const program = new commander.Command();
program
.hook('preAction', async() => {
await 0;
calls.push('before');
})
.action(() => calls.push('action'));
const result = program.parseAsync([], { from: 'user' });
expect(calls).toEqual([]);
await result;
expect(calls).toEqual(['before', 'action']);
});
test('when async postAction then async from postAction', async() => {
const calls = [];
const program = new commander.Command();
program
.hook('postAction', async() => {
await 0;
calls.push('after');
})
.action(() => calls.push('action'));
const result = program.parseAsync([], { from: 'user' });
expect(calls).toEqual(['action']);
await result;
expect(calls).toEqual(['action', 'after']);
});
test('when async action then async from action', async() => {
const calls = [];
const program = new commander.Command();
program
.hook('preAction', () => calls.push('before'))
.hook('postAction', () => calls.push('after'))
.action(async() => {
await 0;
calls.push('action');
});
const result = program.parseAsync([], { from: 'user' });
expect(calls).toEqual(['before']);
await result;
expect(calls).toEqual(['before', 'action', 'after']);
});
test('when async first preAction then async from first preAction', async() => {
const calls = [];
const program = new commander.Command();
program
.hook('preAction', async() => {
await 0;
calls.push('1');
})
.hook('preAction', () => calls.push('2'))
.action(() => calls.push('action'));
const result = program.parseAsync([], { from: 'user' });
expect(calls).toEqual([]);
await result;
expect(calls).toEqual(['1', '2', 'action']);
});
test('when async second preAction then async from second preAction', async() => {
const calls = [];
const program = new commander.Command();
program
.hook('preAction', () => calls.push('1'))
.hook('preAction', async() => {
await 0;
calls.push('2');
})
.action(() => calls.push('action'));
const result = program.parseAsync([], { from: 'user' });
expect(calls).toEqual(['1']);
await result;
expect(calls).toEqual(['1', '2', 'action']);
});
test('when async hook everything then hooks called nested', async() => {
const calls = [];
const program = new commander.Command();
program
.hook('preAction', async() => { await 0; calls.push('pb1'); })
.hook('postAction', async() => { await 0; calls.push('pa1'); });
program
.hook('preAction', async() => { await 0; calls.push('pb2'); })
.hook('postAction', async() => { await 0; calls.push('pa2'); });
program.command('sub')
.hook('preAction', async() => { await 0; calls.push('sb'); })
.hook('postAction', async() => { await 0; calls.push('sa'); })
.action(async() => { await 0; calls.push('action'); });
const result = program.parseAsync(['sub'], { from: 'user' });
expect(calls).toEqual([]);
await result;
expect(calls).toEqual(['pb1', 'pb2', 'sb', 'action', 'sa', 'pa2', 'pa1']);
});
test('preSubcommand hook should work', async() => {
const calls = [];
const program = new commander.Command();
program
.hook('preSubcommand', async() => { await 0; calls.push(0); });
program.command('sub')
.action(async() => { await 1; calls.push(1); });
program.action(async() => { await 2; calls.push(2); });
const result = program.parseAsync(['sub'], { from: 'user' });
expect(calls).toEqual([]);
await result;
expect(calls).toEqual([0, 1]);
});
test('preSubcommand hook should effective for direct subcommands', async() => {
const calls = [];
const program = new commander.Command();
program
.hook('preSubcommand', async(thisCommand, subCommand) => {
await 'preSubcommand';
calls.push('preSubcommand');
calls.push(subCommand.name());
});
program
.command('first')
.action(async() => { await 'first'; calls.push('first'); })
.command('second')
.action(async() => { await 'second'; calls.push('second'); })
.command('third')
.action(async() => { await 'third'; calls.push('third'); });
program.action(async() => { await 2; calls.push(2); });
const result = program.parseAsync(['first', 'second', 'third'], { from: 'user' });
expect(calls).toEqual([]);
await result;
expect(calls).toEqual(['preSubcommand', 'first', 'third']);
});
});