command.addCommand.test.js
This file
Path
tests/command.addCommand.test.js
70 lines · 2.2 KiB · JavaScript
Last changed by
Update dependencies, and fix or suppress lint issues (#1376)John Geebefore adoption3d40a2e
History of this fileTake it
Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 1–37why, one step back1const commander = require('../');23// simple sanity check subcommand works4test('when addCommand and specify subcommand then called', () => {5 const program = new commander.Command();6 const leafAction = jest.fn();7 const sub = new commander.Command();8 sub9 .name('sub')10 .action(leafAction);11 program12 .addCommand(sub);1314 program.parse('node test.js sub'.split(' '));15 expect(leafAction).toHaveBeenCalled();16});1718test('when commands added using .addCommand and .command then internals similar', () => {19 const program1 = new commander.Command();20 program1.command('sub');21 const program2 = new commander.Command();22 program2.addCommand(new commander.Command('sub'));2324 // This is a bit of a cheat to check using .addCommand() produces similar result to .command(),25 // since .command() is well tested and understood.2627 const cmd1 = program1.commands[0];28 const cmd2 = program2.commands[0];29 expect(cmd1.parent).toBe(program1);30 expect(cmd2.parent).toBe(program2);3132 for (const key of Object.keys(cmd1)) {33 switch (typeof cmd1[key]) {34 case 'string':35 case 'boolean':36 case 'number':37 case 'undefined':Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b
line 38why, one step back38 // Compare values in a way that will be readable in test failure message.Update dependencies, and fix or suppress lint issues (#1376)John Geebefore adoption3d40a2e
line 39why, one step back39 // eslint-disable-next-line jest/no-conditional-expectParse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 40–70why, one step back40 expect(`${key}:${cmd1[key]}`).toEqual(`${key}:${cmd2[key]}`);41 break;42 }43 }44});4546test('when command without name passed to .addCommand then throw', () => {47 const program = new commander.Command();48 const cmd = new commander.Command();49 expect(() => {50 program.addCommand(cmd);51 }).toThrow();52});5354test('when executable command without custom executableFile passed to .addCommand then throw', () => {55 const program = new commander.Command();56 const cmd = new commander.Command('sub');57 cmd.command('exec', 'exec description');58 expect(() => {59 program.addCommand(cmd);60 }).toThrow();61});6263test('when executable command with custom executableFile passed to .addCommand then ok', () => {64 const program = new commander.Command();65 const cmd = new commander.Command('sub');66 cmd.command('exec', 'exec description', { executableFile: 'custom' });67 expect(() => {68 program.addCommand(cmd);69 }).not.toThrow();70});