Skip to content
Myr

command.addCommand.test.js

This file

Path

tests/command.addCommand.test.js

69 lines · 2.1 KiB · JavaScript

Last changed by

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

History of this file

Take it

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 1–37why, one step back
1const 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 back
38 // Compare values in a way that will be readable in test failure message.

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 39–69why, one step back
39 expect(`${key}:${cmd1[key]}`).toEqual(`${key}:${cmd2[key]}`);
40 break;41 }42 }43});4445test('when command without name passed to .addCommand then throw', () => {46 const program = new commander.Command();47 const cmd = new commander.Command();48 expect(() => {49 program.addCommand(cmd);50 }).toThrow();51});5253test('when executable command without custom executableFile passed to .addCommand then throw', () => {54 const program = new commander.Command();55 const cmd = new commander.Command('sub');56 cmd.command('exec', 'exec description');57 expect(() => {58 program.addCommand(cmd);59 }).toThrow();60});6162test('when executable command with custom executableFile passed to .addCommand then ok', () => {63 const program = new commander.Command();64 const cmd = new commander.Command('sub');65 cmd.command('exec', 'exec description', { executableFile: 'custom' });66 expect(() => {67 program.addCommand(cmd);68 }).not.toThrow();69});