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
Do not mutate program.args when calling action handler (#1048)John Geebefore adoptioncc4a525
lines 1–22why, one step back1const commander = require('../');23// Test some behaviours of .action not covered in more specific tests.45test('when .action called then command passed to action', () => {6 const actionMock = jest.fn();7 const program = new commander.Command();8 const cmd = program9 .command('info')10 .action(actionMock);11 program.parse(['node', 'test', 'info']);12 expect(actionMock).toHaveBeenCalledWith(cmd);13});1415test('when .action called then program.args only contains args', () => {16 // At one time program.args was being modified to contain the same args as the call to .action17 // and so included the command as an extra and unexpected complex item in array.18 const program = new commander.Command();19 program20 .command('info <file>')21 .action(() => {});22 program.parse(['node', 'test', 'info', 'my-file']);parseOptions rework phase 1 (#1138)John Geebefore adoption62234f6
line 23why, one step back23 expect(program.args).toEqual(['info', 'my-file']);Do not mutate program.args when calling action handler (#1048)John Geebefore adoptioncc4a525
lines 24–36why, one step back24});2526test('when .action called with extra arguments then extras also passed to action', () => {27 // This is a new and undocumented behaviour for now.28 // Might make this an error by default in future.29 const actionMock = jest.fn();30 const program = new commander.Command();31 const cmd = program32 .command('info <file>')33 .action(actionMock);34 program.parse(['node', 'test', 'info', 'my-file', 'a']);35 expect(actionMock).toHaveBeenCalledWith('my-file', cmd, ['a']);36});Merge pull request #1063 from shadowspawn/feature/test-mainTomoaki Abebefore adoptiona45d676
line 37why, one step back37Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
line 38why, one step back38test('when .action on program with required argument and argument supplied then action called', () => {Merge pull request #1063 from shadowspawn/feature/test-mainTomoaki Abebefore adoptiona45d676
lines 39–47why, one step back39 const actionMock = jest.fn();40 const program = new commander.Command();41 program42 .arguments('<file>')43 .action(actionMock);44 program.parse(['node', 'test', 'my-file']);45 expect(actionMock).toHaveBeenCalledWith('my-file', program);46});47Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
line 48why, one step back48test('when .action on program with required argument and argument not supplied then action not called', () => {Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 49–50why, one step back49 // Optional. Use internal knowledge to suppress output to keep test output clean.50 const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 51–53why, one step back51 const actionMock = jest.fn();52 const program = new commander.Command();53 programParse rework for nested commands (#1149)John Geebefore adoption1345f98
line 54why, one step back54 .exitOverride()Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 55–56why, one step back55 .arguments('<file>')56 .action(actionMock);Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 57–59why, one step back57 expect(() => {58 program.parse(['node', 'test']);59 }).toThrow();Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
line 60why, one step back60 expect(actionMock).not.toHaveBeenCalled();Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 61why, one step back61 consoleErrorSpy.mockRestore();Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 62–63why, one step back62});63Merge pull request #1063 from shadowspawn/feature/test-mainTomoaki Abebefore adoptiona45d676
lines 64–93why, one step back64// Changes made in #729 to call program action handler65test('when .action on program and no arguments then action called', () => {66 const actionMock = jest.fn();67 const program = new commander.Command();68 program69 .action(actionMock);70 program.parse(['node', 'test']);71 expect(actionMock).toHaveBeenCalledWith(program);72});7374test('when .action on program with optional argument supplied then action called', () => {75 const actionMock = jest.fn();76 const program = new commander.Command();77 program78 .arguments('[file]')79 .action(actionMock);80 program.parse(['node', 'test', 'my-file']);81 expect(actionMock).toHaveBeenCalledWith('my-file', program);82});8384test('when .action on program without optional argument supplied then action called', () => {85 const actionMock = jest.fn();86 const program = new commander.Command();87 program88 .arguments('[file]')89 .action(actionMock);90 program.parse(['node', 'test']);91 expect(actionMock).toHaveBeenCalledWith(undefined, program);92});93Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
line 94why, one step back94test('when .action on program with optional argument and subcommand and program argument then program action called', () => {Merge pull request #1063 from shadowspawn/feature/test-mainTomoaki Abebefore adoptiona45d676
lines 95–105why, one step back95 const actionMock = jest.fn();96 const program = new commander.Command();97 program98 .arguments('[file]')99 .action(actionMock);100 program101 .command('subcommand');102103 program.parse(['node', 'test', 'a']);104105 expect(actionMock).toHaveBeenCalledWith('a', program);Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 106–120why, one step back106});107108// Changes made in #1062 to allow this case109test('when .action on program with optional argument and subcommand and no program argument then program action called', () => {110 const actionMock = jest.fn();111 const program = new commander.Command();112 program113 .arguments('[file]')114 .action(actionMock);115 program116 .command('subcommand');117118 program.parse(['node', 'test']);119120 expect(actionMock).toHaveBeenCalledWith(undefined, program);Merge pull request #1063 from shadowspawn/feature/test-mainTomoaki Abebefore adoptiona45d676
line 121why, one step back121});Merge branch 'develop' into release/5.xJohn Geebefore adoption491b2e4
lines 122–137why, one step back122123test('when action is async then can await parseAsync', async() => {124 let asyncFinished = false;125 async function delay() {126 await new Promise(resolve => setTimeout(resolve, 100));127 asyncFinished = true;128 };129 const program = new commander.Command();130 program131 .action(delay);132133 const later = program.parseAsync(['node', 'test']);134 expect(asyncFinished).toBe(false);135 await later;136 expect(asyncFinished).toBe(true);137});