command.action.test.js
This file
Path
tests/command.action.test.js
122 lines · 4.5 KiB · JavaScript
Last changed by
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
History of this fileTake it
The walk stopped after 200 commits: lines older than f47bdf5 point at it.
Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 1–11why, 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']);Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 12why, one step back12 expect(actionMock).toHaveBeenCalledWith(cmd.opts(), cmd);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 13–14why, one step back13});14Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 15–24why, one step back15test('when .action called then this is set to command', () => {16 const program = new commander.Command();17 let actionThis;18 const cmd = program19 .command('info')20 .action(function() { actionThis = this; });21 program.parse(['node', 'test', 'info']);22 expect(actionThis).toBe(cmd);23});24Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 25–35why, one step back25test('when .action called then program.args only contains args', () => {26 // At one time program.args was being modified to contain the same args as the call to .action27 // and so included the command as an extra and unexpected complex item in array.28 const program = new commander.Command();29 program30 .command('info <file>')31 .action(() => {});32 program.parse(['node', 'test', 'info', 'my-file']);33 expect(program.args).toEqual(['info', 'my-file']);34});35More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 36why, one step back36test.each(getTestCases('<file>'))('when .action on program with required argument via %s and argument supplied then action called', (methodName, program) => {Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
line 37why, one step back37 const actionMock = jest.fn();More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 38why, one step back38 program.action(actionMock);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
line 39why, one step back39 program.parse(['node', 'test', 'my-file']);Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 40why, one step back40 expect(actionMock).toHaveBeenCalledWith('my-file', program.opts(), program);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 41–42why, one step back41});42More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 43why, one step back43test.each(getTestCases('<file>'))('when .action on program with required argument via %s and argument not supplied then action not called', (methodName, program) => {Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 44–46why, one step back44 const actionMock = jest.fn();45 program46 .exitOverride()configure output (#1387)John Geebefore adoptioned7f13e
line 47why, one step back47 .configureOutput({ writeErr: () => {} })Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 48–61why, one step back48 .action(actionMock);49 expect(() => {50 program.parse(['node', 'test']);51 }).toThrow();52 expect(actionMock).not.toHaveBeenCalled();53});5455// Changes made in #729 to call program action handler56test('when .action on program and no arguments then action called', () => {57 const actionMock = jest.fn();58 const program = new commander.Command();59 program60 .action(actionMock);61 program.parse(['node', 'test']);Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 62why, one step back62 expect(actionMock).toHaveBeenCalledWith(program.opts(), program);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 63–64why, one step back63});64More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 65why, one step back65test.each(getTestCases('[file]'))('when .action on program with optional argument via %s supplied then action called', (methodName, program) => {Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
line 66why, one step back66 const actionMock = jest.fn();More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 67why, one step back67 program.action(actionMock);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
line 68why, one step back68 program.parse(['node', 'test', 'my-file']);Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 69why, one step back69 expect(actionMock).toHaveBeenCalledWith('my-file', program.opts(), program);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 70–71why, one step back70});71More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 72why, one step back72test.each(getTestCases('[file]'))('when .action on program without optional argument supplied then action called', (methodName, program) => {Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
line 73why, one step back73 const actionMock = jest.fn();More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 74why, one step back74 program.action(actionMock);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
line 75why, one step back75 program.parse(['node', 'test']);Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 76why, one step back76 expect(actionMock).toHaveBeenCalledWith(undefined, program.opts(), program);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 77–78why, one step back77});78More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 79why, one step back79test.each(getTestCases('[file]'))('when .action on program with optional argument via %s and subcommand and program argument then program action called', (methodName, program) => {Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
line 80why, one step back80 const actionMock = jest.fn();More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 81why, one step back81 program.action(actionMock);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 82–86why, one step back82 program83 .command('subcommand');8485 program.parse(['node', 'test', 'a']);86Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 87why, one step back87 expect(actionMock).toHaveBeenCalledWith('a', program.opts(), program);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 88–90why, one step back88});8990// Changes made in #1062 to allow this caseMore consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 91why, one step back91test.each(getTestCases('[file]'))('when .action on program with optional argument via %s and subcommand and no program argument then program action called', (methodName, program) => {Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
line 92why, one step back92 const actionMock = jest.fn();More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
lines 93–94why, one step back93 program.action(actionMock);94 program.command('subcommand');Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 95–97why, one step back9596 program.parse(['node', 'test']);97Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 98why, one step back98 expect(actionMock).toHaveBeenCalledWith(undefined, program.opts(), program);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 99–105why, one step back99});100101test('when action is async then can await parseAsync', async() => {102 let asyncFinished = false;103 async function delay() {104 await new Promise(resolve => setTimeout(resolve, 100));105 asyncFinished = true;Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 106why, one step back106 }Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 107–115why, one step back107 const program = new commander.Command();108 program109 .action(delay);110111 const later = program.parseAsync(['node', 'test']);112 expect(asyncFinished).toBe(false);113 await later;114 expect(asyncFinished).toBe(true);115});More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
lines 116–122why, one step back116117function getTestCases(arg) {118 const withArguments = new commander.Command().arguments(arg);119 const withArgument = new commander.Command().argument(arg);120 const withAddArgument = new commander.Command().addArgument(new commander.Argument(arg));121 return [['.arguments', withArguments], ['.argument', withArgument], ['.addArgument', withAddArgument]];122}