command.hook.test.js
This file
Path
tests/command.hook.test.js
417 lines · 13.6 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
The walk stopped after 200 commits: lines older than 471ec40 point at it.
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–3why, one step back1import * as commander from '../index.js';2import { test, describe } from 'node:test';3import assert from 'node:assert/strict';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 4why, one step back4Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 5–6why, one step back5describe('Command.hook()', () => {6 test('when hook event wrong then throw', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 7why, one step back7 const program = new commander.Command();Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 8–10why, one step back8 assert.throws(() => {9 program.hook('silly', () => {});10 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 11–12why, one step back11 });12Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 13–14why, one step back13 test('when no action then action hooks not called', (t) => {14 const hook = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 15why, one step back15 const program = new commander.Command();Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 16why, one step back16 program.hook('preAction', hook).hook('postAction', hook);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 17why, one step back17 program.parse([], { from: 'user' });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 18why, one step back18 assert.equal(hook.mock.callCount(), 0);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 19–20why, one step back19 });20Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 21–30why, one step back21 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 31why, one step back31Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 32–40why, one step back32 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 41why, one step back41Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 42–51why, one step back42 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 52why, one step back52Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 53–62why, one step back53 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 63why, one step back63Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 64–74why, one step back64 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 75why, one step back75Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 76–86why, one step back76 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 87why, one step back87Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 88–112why, one step back88 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 113–114why, one step back113 });114Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 115–125why, one step back115 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 126why, one step back126Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 127–166why, one step back127 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 back167 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 back168 });169 program.command('sub').action(() => {});170 program.parse(['sub', '--debug'], { from: 'user' });171 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 172why, one step back172Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 173–182why, one step back173 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 back183 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 184why, one step back184Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 185–193why, one step back185 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 back194 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 195why, one step back195Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 196–206why, one step back196 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 207why, one step back207Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 208–222why, one step back208 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 back223 assert.deepEqual(actionCommand.args, ['value']);Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 224–230why, one step back224 });225 program226 .command('sub')227 .argument('<arg>')228 .action(() => {});229 program.parse(['sub', 'value'], { from: 'user' });230 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 231–232why, one step back231 });232Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 233–246why, one step back233 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 back247 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 248why, one step back248Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 249–261why, one step back249 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 back262 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 263why, one step back263Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 264–278why, one step back264 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 279why, one step back279Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 280–294why, one step back280 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 295why, one step back295Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 296–310why, one step back296 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 311why, one step back311Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 312–359why, one step back312 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 360why, one step back360Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 361–364why, one step back361 test('preSubcommand hook should work', async () => {362 const calls = [];363 const program = new commander.Command();364 program.hook('preSubcommand', async () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 365why, one step back365 await 0;Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 366why, one step back366 calls.push(0);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 367why, one step back367 });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 368–370why, one step back368 program.command('sub').action(async () => {369 await 1;370 calls.push(1);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 371why, one step back371 });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 372–374why, one step back372 program.action(async () => {373 await 2;374 calls.push(2);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 375why, one step back375 });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 376–379why, one step back376 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 back380 });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 381–387why, one step back381 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 back388 });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 389–414why, one step back389 program390 .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 back415 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 416–417why, one step back416 });417});