command.addCommand.test.js
This file
Path
tests/command.addCommand.test.js
58 lines · 1.9 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
The walk stopped after 200 commits: lines older than 2911e0e point at it.
docs: update SECURITY.mdabetomobefore adoption2911e0e
lines 1–7why, 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();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 8–9why, one step back8 sub.name('sub').action(leafAction);9 program.addCommand(sub);docs: update SECURITY.mdabetomobefore adoption2911e0e
lines 10–58why, one step back1011 program.parse('node test.js sub'.split(' '));12 expect(leafAction).toHaveBeenCalled();13});1415test('when commands added using .addCommand and .command then internals similar', () => {16 const program1 = new commander.Command();17 program1.command('sub');18 const program2 = new commander.Command();19 program2.addCommand(new commander.Command('sub'));2021 // This is a bit of a cheat to check using .addCommand() produces similar result to .command(),22 // since .command() is well tested and understood.2324 const cmd1 = program1.commands[0];25 const cmd2 = program2.commands[0];26 expect(cmd1.parent).toBe(program1);27 expect(cmd2.parent).toBe(program2);2829 for (const key of Object.keys(cmd1)) {30 switch (typeof cmd1[key]) {31 case 'string':32 case 'boolean':33 case 'number':34 case 'undefined':35 // Compare values in a way that will be readable in test failure message.36 // eslint-disable-next-line jest/no-conditional-expect37 expect(`${key}:${cmd1[key]}`).toEqual(`${key}:${cmd2[key]}`);38 break;39 }40 }41});4243test('when command without name passed to .addCommand then throw', () => {44 const program = new commander.Command();45 const cmd = new commander.Command();46 expect(() => {47 program.addCommand(cmd);48 }).toThrow();49});5051test('when executable command with custom executableFile passed to .addCommand then ok', () => {52 const program = new commander.Command();53 const cmd = new commander.Command('sub');54 cmd.command('exec', 'exec description', { executableFile: 'custom' });55 expect(() => {56 program.addCommand(cmd);57 }).not.toThrow();58});