command.action.test.js
This file
Path
tests/command.action.test.js
90 lines · 3.0 KiB · JavaScript
Last changed by
Merge pull request #1063 from shadowspawn/feature/test-mainTomoaki Abebefore adoptiona45d676
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091const commander = require('../');
// Test some behaviours of .action not covered in more specific tests.
test('when .action called then command passed to action', () => { const actionMock = jest.fn(); const program = new commander.Command(); const cmd = program .command('info') .action(actionMock); program.parse(['node', 'test', 'info']); expect(actionMock).toHaveBeenCalledWith(cmd);});
test('when .action called then program.args only contains args', () => { // At one time program.args was being modified to contain the same args as the call to .action // and so included the command as an extra and unexpected complex item in array. const program = new commander.Command(); program .command('info <file>') .action(() => {}); program.parse(['node', 'test', 'info', 'my-file']); expect(program.args).toEqual(['my-file']);});
test('when .action called with extra arguments then extras also passed to action', () => { // This is a new and undocumented behaviour for now. // Might make this an error by default in future. const actionMock = jest.fn(); const program = new commander.Command(); const cmd = program .command('info <file>') .action(actionMock); program.parse(['node', 'test', 'info', 'my-file', 'a']); expect(actionMock).toHaveBeenCalledWith('my-file', cmd, ['a']);});
test('when .action on program with argument then action called', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('<file>') .action(actionMock); program.parse(['node', 'test', 'my-file']); expect(actionMock).toHaveBeenCalledWith('my-file', program);});
// Changes made in #729 to call program action handlertest('when .action on program and no arguments then action called', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .action(actionMock); program.parse(['node', 'test']); expect(actionMock).toHaveBeenCalledWith(program);});
test('when .action on program with optional argument supplied then action called', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('[file]') .action(actionMock); program.parse(['node', 'test', 'my-file']); expect(actionMock).toHaveBeenCalledWith('my-file', program);});
test('when .action on program without optional argument supplied then action called', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('[file]') .action(actionMock); program.parse(['node', 'test']); expect(actionMock).toHaveBeenCalledWith(undefined, program);});
test('when .action on program with subcommand and program argument then program action called', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('[file]') .action(actionMock); program .command('subcommand');
program.parse(['node', 'test', 'a']);
expect(actionMock).toHaveBeenCalledWith('a', program);});