command.addCommand.test.js
This file
Path
tests/command.addCommand.test.js
69 lines · 2.1 KiB · JavaScript
Last changed by
Parse rework for nested commands (#1149)John Geebefore adoption1345f98
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970const commander = require('../');
// simple sanity check subcommand workstest('when addCommand and specify subcommand then called', () => { const program = new commander.Command(); const leafAction = jest.fn(); const sub = new commander.Command(); sub .name('sub') .action(leafAction); program .addCommand(sub);
program.parse('node test.js sub'.split(' ')); expect(leafAction).toHaveBeenCalled();});
test('when commands added using .addCommand and .command then internals similar', () => { const program1 = new commander.Command(); program1.command('sub'); const program2 = new commander.Command(); program2.addCommand(new commander.Command('sub'));
// This is a bit of a cheat to check using .addCommand() produces similar result to .command(), // since .command() is well tested and understood.
const cmd1 = program1.commands[0]; const cmd2 = program2.commands[0]; expect(cmd1.parent).toBe(program1); expect(cmd2.parent).toBe(program2);
for (const key of Object.keys(cmd1)) { switch (typeof cmd1[key]) { case 'string': case 'boolean': case 'number': case 'undefined': // Compare vaues in a way that will be readable in test failure message. expect(`${key}:${cmd1[key]}`).toEqual(`${key}:${cmd2[key]}`); break; } }});
test('when command without name passed to .addCommand then throw', () => { const program = new commander.Command(); const cmd = new commander.Command(); expect(() => { program.addCommand(cmd); }).toThrow();});
test('when executable command without custom executableFile passed to .addCommand then throw', () => { const program = new commander.Command(); const cmd = new commander.Command('sub'); cmd.command('exec', 'exec description'); expect(() => { program.addCommand(cmd); }).toThrow();});
test('when executable command with custom executableFile passed to .addCommand then ok', () => { const program = new commander.Command(); const cmd = new commander.Command('sub'); cmd.command('exec', 'exec description', { executableFile: 'custom' }); expect(() => { program.addCommand(cmd); }).not.toThrow();});