Skip to content
Myr

command.hook.test.js

This file

Path

tests/command.hook.test.js

359 lines · 12.1 KiB · JavaScript

Last changed by

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

History of this file

Take it

Add hooks for life cycle events: using .hook() (#1514)John Geebefore adoptionc20284d

lines 1–322why, 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();13 program14 .hook('preAction', hook)15 .hook('postAction', hook);16 program.parse([], { from: 'user' });17 expect(hook).not.toHaveBeenCalled();18});1920describe('action hooks with synchronous hooks, order', () => {21 test('when hook preAction then hook called before action', () => {22 const calls = [];23 const program = new commander.Command();24 program25 .hook('preAction', () => calls.push('before'))26 .action(() => calls.push('action'));27 program.parse([], { from: 'user' });28 expect(calls).toEqual(['before', 'action']);29 });3031 test('when hook postAction then hook called after action', () => {32 const calls = [];33 const program = new commander.Command();34 program35 .hook('postAction', () => calls.push('after'))36 .action(() => calls.push('action'));37 program.parse([], { from: 'user' });38 expect(calls).toEqual(['action', 'after']);39 });4041 test('when hook preAction twice then hooks called FIFO', () => {42 const calls = [];43 const program = new commander.Command();44 program45 .hook('preAction', () => calls.push('1'))46 .hook('preAction', () => calls.push('2'))47 .action(() => calls.push('action'));48 program.parse([], { from: 'user' });49 expect(calls).toEqual(['1', '2', 'action']);50 });5152 test('when hook postAction twice then hooks called LIFO', () => {53 const calls = [];54 const program = new commander.Command();55 program56 .hook('postAction', () => calls.push('1'))57 .hook('postAction', () => calls.push('2'))58 .action(() => calls.push('action'));59 program.parse([], { from: 'user' });60 expect(calls).toEqual(['action', '2', '1']);61 });6263 test('when hook preAction at program and sub then hooks called program then sub', () => {64 const calls = [];65 const program = new commander.Command();66 program67 .hook('preAction', () => calls.push('program'));68 program.command('sub')69 .hook('preAction', () => calls.push('sub'))70 .action(() => calls.push('action'));71 program.parse(['sub'], { from: 'user' });72 expect(calls).toEqual(['program', 'sub', 'action']);73 });7475 test('when hook postAction at program and sub then hooks called sub then program', () => {76 const calls = [];77 const program = new commander.Command();78 program79 .hook('postAction', () => calls.push('program'));80 program.command('sub')81 .hook('postAction', () => calls.push('sub'))82 .action(() => calls.push('action'));83 program.parse(['sub'], { from: 'user' });84 expect(calls).toEqual(['action', 'sub', 'program']);85 });8687 test('when hook everything then hooks called nested', () => {88 const calls = [];89 const program = new commander.Command();90 program91 .hook('preAction', () => calls.push('pb1'))92 .hook('postAction', () => calls.push('pa1'));93 program94 .hook('preAction', () => calls.push('pb2'))95 .hook('postAction', () => calls.push('pa2'));96 program.command('sub')97 .hook('preAction', () => calls.push('sb'))98 .hook('postAction', () => calls.push('sa'))99 .action(() => calls.push('action'));100 program.parse(['sub'], { from: 'user' });101 expect(calls).toEqual(['pb1', 'pb2', 'sb', 'action', 'sa', 'pa2', 'pa1']);102 });103});104105describe('action hooks context', () => {106 test('when hook on program then passed program/program', () => {107 const hook = jest.fn();108 const program = new commander.Command();109 program110 .hook('preAction', hook)111 .action(() => {});112 program.parse([], { from: 'user' });113 expect(hook).toHaveBeenCalledWith(program, program);114 });115116 test('when hook on program and call sub then passed program/sub', () => {117 const hook = jest.fn();118 const program = new commander.Command();119 program120 .hook('preAction', hook);121 const sub = program.command('sub')122 .action(() => {});123 program.parse(['sub'], { from: 'user' });124 expect(hook).toHaveBeenCalledWith(program, sub);125 });126127 test('when hook on sub and call sub then passed sub/sub', () => {128 const hook = jest.fn();129 const program = new commander.Command();130 const sub = program.command('sub')131 .hook('preAction', hook)132 .action(() => {});133 program.parse(['sub'], { from: 'user' });134 expect(hook).toHaveBeenCalledWith(sub, sub);135 });136137 test('when hook program on preAction then thisCommand has options set', () => {138 expect.assertions(1);139 const program = new commander.Command();140 program141 .option('--debug')142 .hook('preAction', (thisCommand) => {143 expect(thisCommand.opts().debug).toEqual(true);144 })145 .action(() => {});146 program.parse(['--debug'], { from: 'user' });147 });148149 test('when hook program on preAction and call sub then thisCommand has program options set', () => {150 expect.assertions(1);151 const program = new commander.Command();152 program153 .option('--debug')154 .hook('preAction', (thisCommand) => {155 expect(thisCommand.opts().debug).toEqual(true);156 });157 program.command('sub')158 .action(() => {});159 program.parse(['sub', '--debug'], { from: 'user' });160 });161162 test('when hook program on preAction and call sub then actionCommand has sub options set', () => {163 expect.assertions(1);164 const program = new commander.Command();165 program166 .hook('preAction', (thisCommand, actionCommand) => {167 expect(actionCommand.opts().debug).toEqual(true);168 });169 program.command('sub')170 .option('--debug')171 .action(() => {});172 program.parse(['sub', '--debug'], { from: 'user' });173 });174175 test('when hook program on preAction then actionCommand has args set', () => {176 expect.assertions(1);177 const program = new commander.Command();178 program179 .argument('[arg]')180 .hook('preAction', (thisCommand, actionCommand) => {181 expect(actionCommand.args).toEqual(['value']);182 })183 .action(() => {});184 program.parse(['value'], { from: 'user' });185 });186187 test('when hook program on preAction then actionCommand has args set with options removed', () => {188 expect.assertions(1);189 const program = new commander.Command();190 program191 .argument('[arg]')192 .option('--debug')193 .hook('preAction', (thisCommand, actionCommand) => {194 expect(actionCommand.args).toEqual(['value']);195 })196 .action(() => {});197 program.parse(['value', '--debug'], { from: 'user' });198 });199200 test('when hook program on preAction and call sub then thisCommand has program args set', () => {201 expect.assertions(1);202 const program = new commander.Command();203 program204 .argument('[arg]')205 .hook('preAction', (thisCommand) => {206 expect(thisCommand.args).toEqual(['sub', 'value']);207 });208 program.command('sub')209 .action(() => {});210 program.parse(['sub', 'value'], { from: 'user' });211 });212213 test('when hook program on preAction and call sub then actionCommand has sub args set', () => {214 expect.assertions(1);215 const program = new commander.Command();216 program217 .hook('preAction', (thisCommand, actionCommand) => {218 expect(actionCommand.args).toEqual(['value']);219 });220 program.command('sub')221 .action(() => {});222 program.parse(['sub', 'value'], { from: 'user' });223 });224});225226describe('action hooks async', () => {227 test('when async preAction then async from preAction', async() => {228 const calls = [];229 const program = new commander.Command();230 program231 .hook('preAction', async() => {232 await 0;233 calls.push('before');234 })235 .action(() => calls.push('action'));236 const result = program.parseAsync([], { from: 'user' });237 expect(calls).toEqual([]);238 await result;239 expect(calls).toEqual(['before', 'action']);240 });241242 test('when async postAction then async from postAction', async() => {243 const calls = [];244 const program = new commander.Command();245 program246 .hook('postAction', async() => {247 await 0;248 calls.push('after');249 })250 .action(() => calls.push('action'));251 const result = program.parseAsync([], { from: 'user' });252 expect(calls).toEqual(['action']);253 await result;254 expect(calls).toEqual(['action', 'after']);255 });256257 test('when async action then async from action', async() => {258 const calls = [];259 const program = new commander.Command();260 program261 .hook('preAction', () => calls.push('before'))262 .hook('postAction', () => calls.push('after'))263 .action(async() => {264 await 0;265 calls.push('action');266 });267 const result = program.parseAsync([], { from: 'user' });268 expect(calls).toEqual(['before']);269 await result;270 expect(calls).toEqual(['before', 'action', 'after']);271 });272273 test('when async first preAction then async from first preAction', async() => {274 const calls = [];275 const program = new commander.Command();276 program277 .hook('preAction', async() => {278 await 0;279 calls.push('1');280 })281 .hook('preAction', () => calls.push('2'))282 .action(() => calls.push('action'));283 const result = program.parseAsync([], { from: 'user' });284 expect(calls).toEqual([]);285 await result;286 expect(calls).toEqual(['1', '2', 'action']);287 });288289 test('when async second preAction then async from second preAction', async() => {290 const calls = [];291 const program = new commander.Command();292 program293 .hook('preAction', () => calls.push('1'))294 .hook('preAction', async() => {295 await 0;296 calls.push('2');297 })298 .action(() => calls.push('action'));299 const result = program.parseAsync([], { from: 'user' });300 expect(calls).toEqual(['1']);301 await result;302 expect(calls).toEqual(['1', '2', 'action']);303 });304305 test('when async hook everything then hooks called nested', async() => {306 const calls = [];307 const program = new commander.Command();308 program309 .hook('preAction', async() => { await 0; calls.push('pb1'); })310 .hook('postAction', async() => { await 0; calls.push('pa1'); });311 program312 .hook('preAction', async() => { await 0; calls.push('pb2'); })313 .hook('postAction', async() => { await 0; calls.push('pa2'); });314 program.command('sub')315 .hook('preAction', async() => { await 0; calls.push('sb'); })316 .hook('postAction', async() => { await 0; calls.push('sa'); })317 .action(async() => { await 0; calls.push('action'); });318 const result = program.parseAsync(['sub'], { from: 'user' });319 expect(calls).toEqual([]);320 await result;321 expect(calls).toEqual(['pb1', 'pb2', 'sb', 'action', 'sa', 'pa2', 'pa1']);322 });

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

lines 323–358why, one step back
323
324 test('preSubcommand hook should work', async() => {325 const calls = [];326 const program = new commander.Command();327 program328 .hook('preSubcommand', async() => { await 0; calls.push(0); });329 program.command('sub')330 .action(async() => { await 1; calls.push(1); });331 program.action(async() => { await 2; calls.push(2); });332 const result = program.parseAsync(['sub'], { from: 'user' });333 expect(calls).toEqual([]);334 await result;335 expect(calls).toEqual([0, 1]);336 });337 test('preSubcommand hook should effective for direct subcommands', async() => {338 const calls = [];339 const program = new commander.Command();340 program341 .hook('preSubcommand', async(thisCommand, subCommand) => {342 await 'preSubcommand';343 calls.push('preSubcommand');344 calls.push(subCommand.name());345 });346 program347 .command('first')348 .action(async() => { await 'first'; calls.push('first'); })349 .command('second')350 .action(async() => { await 'second'; calls.push('second'); })351 .command('third')352 .action(async() => { await 'third'; calls.push('third'); });353 program.action(async() => { await 2; calls.push(2); });354 const result = program.parseAsync(['first', 'second', 'third'], { from: 'user' });355 expect(calls).toEqual([]);356 await result;357 expect(calls).toEqual(['preSubcommand', 'first', 'third']);358 });

Add hooks for life cycle events: using .hook() (#1514)John Geebefore adoptionc20284d

line 359why, one step back
359});