Skip to content
Myr

command.hook.test.js

This file

Path

tests/command.hook.test.js

395 lines · 12.3 KiB · JavaScript

Last changed by

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

History of this file

Take it

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

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 1–12why, one step back
1const commander = require('../');
23test('when hook event wrong then throw', () => {4 const program = new commander.Command();5 expect(() => {6 program.hook('silly', () => {});7 }).toThrow();8});910test('when no action then action hooks not called', () => {11 const hook = jest.fn();12 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

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

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 14–63why, one step back
14 program.parse([], { from: 'user' });
15 expect(hook).not.toHaveBeenCalled();16});1718describe('action hooks with synchronous hooks, order', () => {19 test('when hook preAction then hook called before action', () => {20 const calls = [];21 const program = new commander.Command();22 program23 .hook('preAction', () => calls.push('before'))24 .action(() => calls.push('action'));25 program.parse([], { from: 'user' });26 expect(calls).toEqual(['before', 'action']);27 });2829 test('when hook postAction then hook called after action', () => {30 const calls = [];31 const program = new commander.Command();32 program33 .hook('postAction', () => calls.push('after'))34 .action(() => calls.push('action'));35 program.parse([], { from: 'user' });36 expect(calls).toEqual(['action', 'after']);37 });3839 test('when hook preAction twice then hooks called FIFO', () => {40 const calls = [];41 const program = new commander.Command();42 program43 .hook('preAction', () => calls.push('1'))44 .hook('preAction', () => calls.push('2'))45 .action(() => calls.push('action'));46 program.parse([], { from: 'user' });47 expect(calls).toEqual(['1', '2', 'action']);48 });4950 test('when hook postAction twice then hooks called LIFO', () => {51 const calls = [];52 const program = new commander.Command();53 program54 .hook('postAction', () => calls.push('1'))55 .hook('postAction', () => calls.push('2'))56 .action(() => calls.push('action'));57 program.parse([], { from: 'user' });58 expect(calls).toEqual(['action', '2', '1']);59 });6061 test('when hook preAction at program and sub then hooks called program then sub', () => {62 const calls = [];63 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 64why, one step back
64 program.hook('preAction', () => calls.push('program'));

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 65why, one step back
65 program

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 66why, one step back
66 .command('sub')

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 67–75why, one step back
67 .hook('preAction', () => calls.push('sub'))
68 .action(() => calls.push('action'));69 program.parse(['sub'], { from: 'user' });70 expect(calls).toEqual(['program', 'sub', 'action']);71 });7273 test('when hook postAction at program and sub then hooks called sub then program', () => {74 const calls = [];75 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 76why, one step back
76 program.hook('postAction', () => calls.push('program'));

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 77why, one step back
77 program

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 78why, one step back
78 .command('sub')

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 79–93why, one step back
79 .hook('postAction', () => calls.push('sub'))
80 .action(() => calls.push('action'));81 program.parse(['sub'], { from: 'user' });82 expect(calls).toEqual(['action', 'sub', 'program']);83 });8485 test('when hook everything then hooks called nested', () => {86 const calls = [];87 const program = new commander.Command();88 program89 .hook('preAction', () => calls.push('pb1'))90 .hook('postAction', () => calls.push('pa1'));91 program92 .hook('preAction', () => calls.push('pb2'))93 .hook('postAction', () => calls.push('pa2'));

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 94–95why, one step back
94 program
95 .command('sub')

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 96–107why, one step back
96 .hook('preAction', () => calls.push('sb'))
97 .hook('postAction', () => calls.push('sa'))98 .action(() => calls.push('action'));99 program.parse(['sub'], { from: 'user' });100 expect(calls).toEqual(['pb1', 'pb2', 'sb', 'action', 'sa', 'pa2', 'pa1']);101 });102});103104describe('action hooks context', () => {105 test('when hook on program then passed program/program', () => {106 const hook = jest.fn();107 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 108why, one step back
108 program.hook('preAction', hook).action(() => {});

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 109–115why, one step back
109 program.parse([], { from: 'user' });
110 expect(hook).toHaveBeenCalledWith(program, program);111 });112113 test('when hook on program and call sub then passed program/sub', () => {114 const hook = jest.fn();115 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 116–117why, one step back
116 program.hook('preAction', hook);
117 const sub = program.command('sub').action(() => {});

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 118–124why, one step back
118 program.parse(['sub'], { from: 'user' });
119 expect(hook).toHaveBeenCalledWith(program, sub);120 });121122 test('when hook on sub and call sub then passed sub/sub', () => {123 const hook = jest.fn();124 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 125–126why, one step back
125 const sub = program
126 .command('sub')

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 127–147why, one step back
127 .hook('preAction', hook)
128 .action(() => {});129 program.parse(['sub'], { from: 'user' });130 expect(hook).toHaveBeenCalledWith(sub, sub);131 });132133 test('when hook program on preAction then thisCommand has options set', () => {134 expect.assertions(1);135 const program = new commander.Command();136 program137 .option('--debug')138 .hook('preAction', (thisCommand) => {139 expect(thisCommand.opts().debug).toEqual(true);140 })141 .action(() => {});142 program.parse(['--debug'], { from: 'user' });143 });144145 test('when hook program on preAction and call sub then thisCommand has program options set', () => {146 expect.assertions(1);147 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 148–151why, one step back
148 program.option('--debug').hook('preAction', (thisCommand) => {
149 expect(thisCommand.opts().debug).toEqual(true);150 });151 program.command('sub').action(() => {});

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 152–157why, one step back
152 program.parse(['sub', '--debug'], { from: 'user' });
153 });154155 test('when hook program on preAction and call sub then actionCommand has sub options set', () => {156 expect.assertions(1);157 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 158–160why, one step back
158 program.hook('preAction', (thisCommand, actionCommand) => {
159 expect(actionCommand.opts().debug).toEqual(true);160 });

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 161why, one step back
161 program

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 162why, one step back
162 .command('sub')

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 163–195why, one step back
163 .option('--debug')
164 .action(() => {});165 program.parse(['sub', '--debug'], { from: 'user' });166 });167168 test('when hook program on preAction then actionCommand has args set', () => {169 expect.assertions(1);170 const program = new commander.Command();171 program172 .argument('[arg]')173 .hook('preAction', (thisCommand, actionCommand) => {174 expect(actionCommand.args).toEqual(['value']);175 })176 .action(() => {});177 program.parse(['value'], { from: 'user' });178 });179180 test('when hook program on preAction then actionCommand has args set with options removed', () => {181 expect.assertions(1);182 const program = new commander.Command();183 program184 .argument('[arg]')185 .option('--debug')186 .hook('preAction', (thisCommand, actionCommand) => {187 expect(actionCommand.args).toEqual(['value']);188 })189 .action(() => {});190 program.parse(['value', '--debug'], { from: 'user' });191 });192193 test('when hook program on preAction and call sub then thisCommand has program args set', () => {194 expect.assertions(1);195 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 196–198why, one step back
196 program.argument('[arg]').hook('preAction', (thisCommand) => {
197 expect(thisCommand.args).toEqual(['sub', 'value']);198 });

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

lines 199–202why, one step back
199 program
200 .command('sub')201 .argument('<arg>')202 .action(() => {});

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 203–208why, one step back
203 program.parse(['sub', 'value'], { from: 'user' });
204 });205206 test('when hook program on preAction and call sub then actionCommand has sub args set', () => {207 expect.assertions(1);208 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 209–211why, one step back
209 program.hook('preAction', (thisCommand, actionCommand) => {
210 expect(actionCommand.args).toEqual(['value']);211 });

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

lines 212–215why, one step back
212 program
213 .command('sub')214 .argument('<arg>')215 .action(() => {});

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 216–220why, one step back
216 program.parse(['sub', 'value'], { from: 'user' });
217 });218});219220describe('action hooks async', () => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 221why, one step back
221 test('when async preAction then async from preAction', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 222–224why, one step back
222 const calls = [];
223 const program = new commander.Command();224 program

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 225why, one step back
225 .hook('preAction', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 226–235why, one step back
226 await 0;
227 calls.push('before');228 })229 .action(() => calls.push('action'));230 const result = program.parseAsync([], { from: 'user' });231 expect(calls).toEqual([]);232 await result;233 expect(calls).toEqual(['before', 'action']);234 });235

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 236why, one step back
236 test('when async postAction then async from postAction', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 237–239why, one step back
237 const calls = [];
238 const program = new commander.Command();239 program

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 240why, one step back
240 .hook('postAction', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 241–250why, one step back
241 await 0;
242 calls.push('after');243 })244 .action(() => calls.push('action'));245 const result = program.parseAsync([], { from: 'user' });246 expect(calls).toEqual(['action']);247 await result;248 expect(calls).toEqual(['action', 'after']);249 });250

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 251why, one step back
251 test('when async action then async from action', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 252–256why, one step back
252 const calls = [];
253 const program = new commander.Command();254 program255 .hook('preAction', () => calls.push('before'))256 .hook('postAction', () => calls.push('after'))

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 257why, one step back
257 .action(async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 258–266why, one step back
258 await 0;
259 calls.push('action');260 });261 const result = program.parseAsync([], { from: 'user' });262 expect(calls).toEqual(['before']);263 await result;264 expect(calls).toEqual(['before', 'action', 'after']);265 });266

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 267why, one step back
267 test('when async first preAction then async from first preAction', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 268–270why, one step back
268 const calls = [];
269 const program = new commander.Command();270 program

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 271why, one step back
271 .hook('preAction', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 272–282why, one step back
272 await 0;
273 calls.push('1');274 })275 .hook('preAction', () => calls.push('2'))276 .action(() => calls.push('action'));277 const result = program.parseAsync([], { from: 'user' });278 expect(calls).toEqual([]);279 await result;280 expect(calls).toEqual(['1', '2', 'action']);281 });282

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 283why, one step back
283 test('when async second preAction then async from second preAction', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 284–287why, one step back
284 const calls = [];
285 const program = new commander.Command();286 program287 .hook('preAction', () => calls.push('1'))

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 288why, one step back
288 .hook('preAction', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 289–298why, one step back
289 await 0;
290 calls.push('2');291 })292 .action(() => calls.push('action'));293 const result = program.parseAsync([], { from: 'user' });294 expect(calls).toEqual(['1']);295 await result;296 expect(calls).toEqual(['1', '2', 'action']);297 });298

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 299why, one step back
299 test('when async hook everything then hooks called nested', async () => {

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 300–302why, one step back
300 const calls = [];
301 const program = new commander.Command();302 program

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 303–310why, one step back
303 .hook('preAction', async () => {
304 await 0;305 calls.push('pb1');306 })307 .hook('postAction', async () => {308 await 0;309 calls.push('pa1');310 });

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 311why, one step back
311 program

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 312–333why, one step back
312 .hook('preAction', async () => {
313 await 0;314 calls.push('pb2');315 })316 .hook('postAction', async () => {317 await 0;318 calls.push('pa2');319 });320 program321 .command('sub')322 .hook('preAction', async () => {323 await 0;324 calls.push('sb');325 })326 .hook('postAction', async () => {327 await 0;328 calls.push('sa');329 })330 .action(async () => {331 await 0;332 calls.push('action');333 });

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 334–338why, one step back
334 const result = program.parseAsync(['sub'], { from: 'user' });
335 expect(calls).toEqual([]);336 await result;337 expect(calls).toEqual(['pb1', 'pb2', 'sb', 'action', 'sa', 'pa2', 'pa1']);338 });

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

line 339why, one step back
339

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 340why, one step back
340 test('preSubcommand hook should work', async () => {

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

lines 341–342why, one step back
341 const calls = [];
342 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 343–354why, one step back
343 program.hook('preSubcommand', async () => {
344 await 0;345 calls.push(0);346 });347 program.command('sub').action(async () => {348 await 1;349 calls.push(1);350 });351 program.action(async () => {352 await 2;353 calls.push(2);354 });

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

lines 355–359why, one step back
355 const result = program.parseAsync(['sub'], { from: 'user' });
356 expect(calls).toEqual([]);357 await result;358 expect(calls).toEqual([0, 1]);359 });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 360why, one step back
360 test('preSubcommand hook should effective for direct subcommands', async () => {

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

lines 361–362why, one step back
361 const calls = [];
362 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 363–367why, one step back
363 program.hook('preSubcommand', async (thisCommand, subCommand) => {
364 await 'preSubcommand';365 calls.push('preSubcommand');366 calls.push(subCommand.name());367 });

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

lines 368–369why, one step back
368 program
369 .command('first')

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 370–373why, one step back
370 .action(async () => {
371 await 'first';372 calls.push('first');373 })

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

line 374why, one step back
374 .command('second')

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 375–378why, one step back
375 .action(async () => {
376 await 'second';377 calls.push('second');378 })

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

line 379why, one step back
379 .command('third')

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 380–390why, one step back
380 .action(async () => {
381 await 'third';382 calls.push('third');383 });384 program.action(async () => {385 await 2;386 calls.push(2);387 });388 const result = program.parseAsync(['first', 'second', 'third'], {389 from: 'user',390 });

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

lines 391–394why, one step back
391 expect(calls).toEqual([]);
392 await result;393 expect(calls).toEqual(['preSubcommand', 'first', 'third']);394 });

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 395why, one step back
395});