command.alias.test.js
This file
Path
tests/command.alias.test.js
75 lines · 2.2 KiB · JavaScript
Last changed by
Add support for multiple aliases (#1236)John Geebefore adoption28e8d3f
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576const commander = require('../');
// Running alias commands is tested in command.executableSubcommand.lookup.test.js// Test various other behaviours for .alias
test('when command has alias then appears in help', () => { const program = new commander.Command(); program .command('info [thing]') .alias('i'); const helpInformation = program.helpInformation(); expect(helpInformation).toMatch('info|i');});
test('when command has aliases added separately then only first appears in help', () => { const program = new commander.Command(); program .command('list [thing]') .alias('ls') .alias('dir'); const helpInformation = program.helpInformation(); expect(helpInformation).toMatch('list|ls ');});
test('when command has aliases then only first appears in help', () => { const program = new commander.Command(); program .command('list [thing]') .aliases(['ls', 'dir']); const helpInformation = program.helpInformation(); expect(helpInformation).toMatch('list|ls ');});
test('when command name = alias then error', () => { const program = new commander.Command(); expect(() => { program .command('fail') .alias('fail'); }).toThrow("Command alias can't be the same as its name");});
test('when use alias then action handler called', () => { const program = new commander.Command(); const actionMock = jest.fn(); program .command('list') .alias('ls') .action(actionMock); program.parse(['ls'], { from: 'user' }); expect(actionMock).toHaveBeenCalled();});
test('when use second alias added separately then action handler called', () => { const program = new commander.Command(); const actionMock = jest.fn(); program .command('list') .alias('ls') .alias('dir') .action(actionMock); program.parse(['dir'], { from: 'user' }); expect(actionMock).toHaveBeenCalled();});
test('when use second of aliases then action handler called', () => { const program = new commander.Command(); const actionMock = jest.fn(); program .command('list') .aliases(['ls', 'dir']) .action(actionMock); program.parse(['dir'], { from: 'user' }); expect(actionMock).toHaveBeenCalled();});