Skip to content
Myr

command.hook.test.js

This file

Path

tests/command.hook.test.js

417 lines · 13.6 KiB · JavaScript

Last changed by

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

History of this file

Take it

The walk stopped after 200 commits: lines older than cc2db5b point at it.

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 1why, one step back
1const commander = require('../');

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 2–3why, one step back
2const { test, describe } = require('node:test');
3const assert = require('node:assert/strict');

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 4why, one step back
4

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 5–6why, one step back
5describe('Command.hook()', () => {
6 test('when hook event wrong then throw', () => {

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 7why, one step back
7 const program = new commander.Command();

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 8–10why, one step back
8 assert.throws(() => {
9 program.hook('silly', () => {});10 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 11–12why, one step back
11 });
12

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 13–14why, one step back
13 test('when no action then action hooks not called', (t) => {
14 const hook = t.mock.fn();

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 15why, one step back
15 const program = new commander.Command();

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 16why, one step back
16 program.hook('preAction', hook).hook('postAction', hook);

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 17why, one step back
17 program.parse([], { from: 'user' });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 18why, one step back
18 assert.equal(hook.mock.callCount(), 0);

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 19–20why, one step back
19 });
20

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 21–30why, one step back
21 describe('action hooks with synchronous hooks, order', () => {
22 test('when hook preAction then hook called before action', () => {23 const calls = [];24 const program = new commander.Command();25 program26 .hook('preAction', () => calls.push('before'))27 .action(() => calls.push('action'));28 program.parse([], { from: 'user' });29 assert.deepEqual(calls, ['before', 'action']);30 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 31why, one step back
31

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 32–40why, one step back
32 test('when hook postAction then hook called after action', () => {
33 const calls = [];34 const program = new commander.Command();35 program36 .hook('postAction', () => calls.push('after'))37 .action(() => calls.push('action'));38 program.parse([], { from: 'user' });39 assert.deepEqual(calls, ['action', 'after']);40 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 41why, one step back
41

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 42–51why, one step back
42 test('when hook preAction twice then hooks called FIFO', () => {
43 const calls = [];44 const program = new commander.Command();45 program46 .hook('preAction', () => calls.push('1'))47 .hook('preAction', () => calls.push('2'))48 .action(() => calls.push('action'));49 program.parse([], { from: 'user' });50 assert.deepEqual(calls, ['1', '2', 'action']);51 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 52why, one step back
52

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 53–62why, one step back
53 test('when hook postAction twice then hooks called LIFO', () => {
54 const calls = [];55 const program = new commander.Command();56 program57 .hook('postAction', () => calls.push('1'))58 .hook('postAction', () => calls.push('2'))59 .action(() => calls.push('action'));60 program.parse([], { from: 'user' });61 assert.deepEqual(calls, ['action', '2', '1']);62 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 63why, one step back
63

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 64–74why, one step back
64 test('when hook preAction at program and sub then hooks called program then sub', () => {
65 const calls = [];66 const program = new commander.Command();67 program.hook('preAction', () => calls.push('program'));68 program69 .command('sub')70 .hook('preAction', () => calls.push('sub'))71 .action(() => calls.push('action'));72 program.parse(['sub'], { from: 'user' });73 assert.deepEqual(calls, ['program', 'sub', 'action']);74 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 75why, one step back
75

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 76–86why, one step back
76 test('when hook postAction at program and sub then hooks called sub then program', () => {
77 const calls = [];78 const program = new commander.Command();79 program.hook('postAction', () => calls.push('program'));80 program81 .command('sub')82 .hook('postAction', () => calls.push('sub'))83 .action(() => calls.push('action'));84 program.parse(['sub'], { from: 'user' });85 assert.deepEqual(calls, ['action', 'sub', 'program']);86 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 87why, one step back
87

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 88–112why, one step back
88 test('when hook everything then hooks called nested', () => {
89 const calls = [];90 const program = new commander.Command();91 program92 .hook('preAction', () => calls.push('pb1'))93 .hook('postAction', () => calls.push('pa1'));94 program95 .hook('preAction', () => calls.push('pb2'))96 .hook('postAction', () => calls.push('pa2'));97 program98 .command('sub')99 .hook('preAction', () => calls.push('sb'))100 .hook('postAction', () => calls.push('sa'))101 .action(() => calls.push('action'));102 program.parse(['sub'], { from: 'user' });103 assert.deepEqual(calls, [104 'pb1',105 'pb2',106 'sb',107 'action',108 'sa',109 'pa2',110 'pa1',111 ]);112 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 113–114why, one step back
113 });
114

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 115–125why, one step back
115 describe('action hooks context', () => {
116 test('when hook on program then passed program/program', (t) => {117 const hook = t.mock.fn();118 const program = new commander.Command();119 program.hook('preAction', hook).action(() => {});120 program.parse([], { from: 'user' });121 assert.equal(hook.mock.callCount(), 1);122 const callArgs = hook.mock.calls[0].arguments;123 assert.equal(callArgs[0], program);124 assert.equal(callArgs[1], program);125 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 126why, one step back
126

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 127–166why, one step back
127 test('when hook on program and call sub then passed program/sub', (t) => {
128 const hook = t.mock.fn();129 const program = new commander.Command();130 program.hook('preAction', hook);131 const sub = program.command('sub').action(() => {});132 program.parse(['sub'], { from: 'user' });133 assert.equal(hook.mock.callCount(), 1);134 const callArgs = hook.mock.calls[0].arguments;135 assert.equal(callArgs[0], program);136 assert.equal(callArgs[1], sub);137 });138139 test('when hook on sub and call sub then passed sub/sub', (t) => {140 const hook = t.mock.fn();141 const program = new commander.Command();142 const sub = program143 .command('sub')144 .hook('preAction', hook)145 .action(() => {});146 program.parse(['sub'], { from: 'user' });147 assert.equal(hook.mock.callCount(), 1);148 const callArgs = hook.mock.calls[0].arguments;149 assert.equal(callArgs[0], sub);150 assert.equal(callArgs[1], sub);151 });152153 test('when hook program on preAction then thisCommand has options set', () => {154 const program = new commander.Command();155 program156 .option('--debug')157 .hook('preAction', (thisCommand) => {158 assert.equal(thisCommand.opts().debug, true);159 })160 .action(() => {});161 program.parse(['--debug'], { from: 'user' });162 });163164 test('when hook program on preAction and call sub then thisCommand has program options set', () => {165 const program = new commander.Command();166 program.option('--debug').hook('preAction', (thisCommand) => {

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 167why, one step back
167 assert.equal(thisCommand.opts().debug, true);

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 168–171why, one step back
168 });
169 program.command('sub').action(() => {});170 program.parse(['sub', '--debug'], { from: 'user' });171 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 172why, one step back
172

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 173–182why, one step back
173 test('when hook program on preAction and call sub then actionCommand has sub options set', () => {
174 const program = new commander.Command();175 program.hook('preAction', (thisCommand, actionCommand) => {176 assert.equal(actionCommand.opts().debug, true);177 });178 program179 .command('sub')180 .option('--debug')181 .action(() => {});182 program.parse(['sub', '--debug'], { from: 'user' });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 183why, one step back
183 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 184why, one step back
184

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 185–193why, one step back
185 test('when hook program on preAction then actionCommand has args set', () => {
186 const program = new commander.Command();187 program188 .argument('[arg]')189 .hook('preAction', (thisCommand, actionCommand) => {190 assert.deepEqual(actionCommand.args, ['value']);191 })192 .action(() => {});193 program.parse(['value'], { from: 'user' });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 194why, one step back
194 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 195why, one step back
195

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 196–206why, one step back
196 test('when hook program on preAction then actionCommand has args set with options removed', () => {
197 const program = new commander.Command();198 program199 .argument('[arg]')200 .option('--debug')201 .hook('preAction', (thisCommand, actionCommand) => {202 assert.deepEqual(actionCommand.args, ['value']);203 })204 .action(() => {});205 program.parse(['value', '--debug'], { from: 'user' });206 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 207why, one step back
207

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 208–222why, one step back
208 test('when hook program on preAction and call sub then thisCommand has program args set', () => {
209 const program = new commander.Command();210 program.argument('[arg]').hook('preAction', (thisCommand) => {211 assert.deepEqual(thisCommand.args, ['sub', 'value']);212 });213 program214 .command('sub')215 .argument('<arg>')216 .action(() => {});217 program.parse(['sub', 'value'], { from: 'user' });218 });219220 test('when hook program on preAction and call sub then actionCommand has sub args set', () => {221 const program = new commander.Command();222 program.hook('preAction', (thisCommand, actionCommand) => {

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 223why, one step back
223 assert.deepEqual(actionCommand.args, ['value']);

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 224–230why, one step back
224 });
225 program226 .command('sub')227 .argument('<arg>')228 .action(() => {});229 program.parse(['sub', 'value'], { from: 'user' });230 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 231–232why, one step back
231 });
232

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 233–246why, one step back
233 describe('action hooks async', () => {
234 test('when async preAction then async from preAction', async () => {235 const calls = [];236 const program = new commander.Command();237 program238 .hook('preAction', async () => {239 await 0;240 calls.push('before');241 })242 .action(() => calls.push('action'));243 const result = program.parseAsync([], { from: 'user' });244 assert.deepEqual(calls, []);245 await result;246 assert.deepEqual(calls, ['before', 'action']);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 247why, one step back
247 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 248why, one step back
248

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 249–261why, one step back
249 test('when async postAction then async from postAction', async () => {
250 const calls = [];251 const program = new commander.Command();252 program253 .hook('postAction', async () => {254 await 0;255 calls.push('after');256 })257 .action(() => calls.push('action'));258 const result = program.parseAsync([], { from: 'user' });259 assert.deepEqual(calls, ['action']);260 await result;261 assert.deepEqual(calls, ['action', 'after']);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 262why, one step back
262 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 263why, one step back
263

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 264–278why, one step back
264 test('when async action then async from action', async () => {
265 const calls = [];266 const program = new commander.Command();267 program268 .hook('preAction', () => calls.push('before'))269 .hook('postAction', () => calls.push('after'))270 .action(async () => {271 await 0;272 calls.push('action');273 });274 const result = program.parseAsync([], { from: 'user' });275 assert.deepEqual(calls, ['before']);276 await result;277 assert.deepEqual(calls, ['before', 'action', 'after']);278 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 279why, one step back
279

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 280–294why, one step back
280 test('when async first preAction then async from first preAction', async () => {
281 const calls = [];282 const program = new commander.Command();283 program284 .hook('preAction', async () => {285 await 0;286 calls.push('1');287 })288 .hook('preAction', () => calls.push('2'))289 .action(() => calls.push('action'));290 const result = program.parseAsync([], { from: 'user' });291 assert.deepEqual(calls, []);292 await result;293 assert.deepEqual(calls, ['1', '2', 'action']);294 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 295why, one step back
295

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 296–310why, one step back
296 test('when async second preAction then async from second preAction', async () => {
297 const calls = [];298 const program = new commander.Command();299 program300 .hook('preAction', () => calls.push('1'))301 .hook('preAction', async () => {302 await 0;303 calls.push('2');304 })305 .action(() => calls.push('action'));306 const result = program.parseAsync([], { from: 'user' });307 assert.deepEqual(calls, ['1']);308 await result;309 assert.deepEqual(calls, ['1', '2', 'action']);310 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 311why, one step back
311

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 312–359why, one step back
312 test('when async hook everything then hooks called nested', async () => {
313 const calls = [];314 const program = new commander.Command();315 program316 .hook('preAction', async () => {317 await 0;318 calls.push('pb1');319 })320 .hook('postAction', async () => {321 await 0;322 calls.push('pa1');323 });324 program325 .hook('preAction', async () => {326 await 0;327 calls.push('pb2');328 })329 .hook('postAction', async () => {330 await 0;331 calls.push('pa2');332 });333 program334 .command('sub')335 .hook('preAction', async () => {336 await 0;337 calls.push('sb');338 })339 .hook('postAction', async () => {340 await 0;341 calls.push('sa');342 })343 .action(async () => {344 await 0;345 calls.push('action');346 });347 const result = program.parseAsync(['sub'], { from: 'user' });348 assert.deepEqual(calls, []);349 await result;350 assert.deepEqual(calls, [351 'pb1',352 'pb2',353 'sb',354 'action',355 'sa',356 'pa2',357 'pa1',358 ]);359 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 360why, one step back
360

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 361–364why, one step back
361 test('preSubcommand hook should work', async () => {
362 const calls = [];363 const program = new commander.Command();364 program.hook('preSubcommand', async () => {

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 365why, one step back
365 await 0;

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 366why, one step back
366 calls.push(0);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 367why, one step back
367 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 368–370why, one step back
368 program.command('sub').action(async () => {
369 await 1;370 calls.push(1);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 371why, one step back
371 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 372–374why, one step back
372 program.action(async () => {
373 await 2;374 calls.push(2);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 375why, one step back
375 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 376–379why, one step back
376 const result = program.parseAsync(['sub'], { from: 'user' });
377 assert.deepEqual(calls, []);378 await result;379 assert.deepEqual(calls, [0, 1]);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 380why, one step back
380 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 381–387why, one step back
381 test('preSubcommand hook should effective for direct subcommands', async () => {
382 const calls = [];383 const program = new commander.Command();384 program.hook('preSubcommand', async (thisCommand, subCommand) => {385 await 'preSubcommand';386 calls.push('preSubcommand');387 calls.push(subCommand.name());

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 388why, one step back
388 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 389–414why, one step back
389 program
390 .command('first')391 .action(async () => {392 await 'first';393 calls.push('first');394 })395 .command('second')396 .action(async () => {397 await 'second';398 calls.push('second');399 })400 .command('third')401 .action(async () => {402 await 'third';403 calls.push('third');404 });405 program.action(async () => {406 await 2;407 calls.push(2);408 });409 const result = program.parseAsync(['first', 'second', 'third'], {410 from: 'user',411 });412 assert.deepEqual(calls, []);413 await result;414 assert.deepEqual(calls, ['preSubcommand', 'first', 'third']);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 415why, one step back
415 });

feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687

line 416why, one step back
416 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 417why, one step back
417});