command.addCommand.test.js
This file
Path
tests/command.addCommand.test.js
61 lines · 1.9 KiB · JavaScript
Last changed by
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
History of this fileTake it
The walk stopped after 200 commits: lines older than 20cef03 point at it.
Update supported details for 7.x (#1419)John Geebefore adoption20cef03
lines 1–61why, 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':38 // Compare values in a way that will be readable in test failure message.39 // eslint-disable-next-line jest/no-conditional-expect40 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 with custom executableFile passed to .addCommand then ok', () => {55 const program = new commander.Command();56 const cmd = new commander.Command('sub');57 cmd.command('exec', 'exec description', { executableFile: 'custom' });58 expect(() => {59 program.addCommand(cmd);60 }).not.toThrow();61});