command.action.test.js
This file
Path
tests/command.action.test.js
137 lines · 4.5 KiB · JavaScript
Last changed by
Parse rework for nested commands (#1149)John Geebefore adoption1345f98
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138const 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(['info', '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 required argument and 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 with required argument and argument not supplied then action not called', () => { // Optional. Use internal knowledge to suppress output to keep test output clean. const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { }); const actionMock = jest.fn(); const program = new commander.Command(); program .exitOverride() .arguments('<file>') .action(actionMock); expect(() => { program.parse(['node', 'test']); }).toThrow(); expect(actionMock).not.toHaveBeenCalled(); consoleErrorSpy.mockRestore();});
// 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 optional argument and 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);});
// Changes made in #1062 to allow this casetest('when .action on program with optional argument and subcommand and no 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']);
expect(actionMock).toHaveBeenCalledWith(undefined, program);});
test('when action is async then can await parseAsync', async() => { let asyncFinished = false; async function delay() { await new Promise(resolve => setTimeout(resolve, 100)); asyncFinished = true; }; const program = new commander.Command(); program .action(delay);
const later = program.parseAsync(['node', 'test']); expect(asyncFinished).toBe(false); await later; expect(asyncFinished).toBe(true);});