command.asterisk.test.js
This file
Path
tests/command.asterisk.test.js
164 lines · 5.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–14why, one step back1const commander = require('../');23// .command('*') is the old main/default command handler. It adds a listener4// for 'command:*'. It has been somewhat replaced by the program action handler,5// so most uses are probably old code. Current plan is keep the code backwards compatible6// and put work in elsewhere for new code (e.g. evolving behaviour for program action handler).7//8// The event 'command:*' is also listened for directly for testing for unknown commands9// due to an example in the README.10//11// Historical: the event 'command:*' used to also be shared by the action handler on the program.1213describe(".command('*')", () => {14 test('when no arguments then asterisk action not called', () => {Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 15why, one step back15 const writeMock = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 16–24why, one step back16 const mockAction = jest.fn();17 const program = new commander.Command();18 program19 .exitOverride() // to catch help20 .command('*')21 .action(mockAction);22 try {23 program.parse(['node', 'test']);24 } catch (err) {Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 25why, one step back25Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 26–27why, one step back26 }27 expect(mockAction).not.toHaveBeenCalled();Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 28why, one step back28 writeMock.mockRestore();Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 29–35why, one step back29 });3031 test('when unrecognised argument then asterisk action called', () => {32 const mockAction = jest.fn();33 const program = new commander.Command();34 program35 .command('*')More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 36why, one step back36 .argument('[args...]')Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 37–61why, one step back37 .action(mockAction);38 program.parse(['node', 'test', 'unrecognised-command']);39 expect(mockAction).toHaveBeenCalled();40 });4142 test('when recognised command then asterisk action not called', () => {43 const mockAction = jest.fn();44 const program = new commander.Command();45 program46 .command('install')47 .action(() => { });48 program49 .command('*')50 .action(mockAction);51 program.parse(['node', 'test', 'install']);52 expect(mockAction).not.toHaveBeenCalled();53 });5455 test('when unrecognised command/argument then asterisk action called', () => {56 const mockAction = jest.fn();57 const program = new commander.Command();58 program59 .command('install');60 program61 .command('*')More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 62why, one step back62 .argument('[args...]')Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 63–66why, one step back63 .action(mockAction);64 program.parse(['node', 'test', 'unrecognised-command']);65 expect(mockAction).toHaveBeenCalled();66 });Skip unknown options check if there is a better error to display (#1464)John Geebefore adoption4f78587
lines 67–75why, one step back6768 test('when unrecognised argument and known option then asterisk action called', () => {69 // This tests for a regression between v4 and v5. Known default option should not be rejected by program.70 const mockAction = jest.fn();71 const program = new commander.Command();72 program73 .command('install');74 const star = program75 .command('*')More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 76why, one step back76 .argument('[args...]')Skip unknown options check if there is a better error to display (#1464)John Geebefore adoption4f78587
lines 77–95why, one step back77 .option('-d, --debug')78 .action(mockAction);79 program.parse(['node', 'test', 'unrecognised-command', '--debug']);80 expect(mockAction).toHaveBeenCalled();81 expect(star.opts().debug).toEqual(true);82 });8384 test('when non-command argument and unknown option then error for unknown option', () => {85 // This is a change in behaviour from v2 which did not error, but is consistent with modern better detection of invalid options86 const mockAction = jest.fn();87 const program = new commander.Command();88 program89 .exitOverride()90 .configureOutput({91 writeErr: () => {}92 })93 .command('install');94 program95 .command('*')More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 96why, one step back96 .argument('[args...]')Skip unknown options check if there is a better error to display (#1464)John Geebefore adoption4f78587
lines 97–105why, one step back97 .action(mockAction);98 let caughtErr;99 try {100 program.parse(['node', 'test', 'some-argument', '--unknown']);101 } catch (err) {102 caughtErr = err;103 }104 expect(caughtErr.code).toEqual('commander.unknownOption');105 });Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 106–147why, one step back106});107108// Test .on explicitly rather than assuming covered by .command109describe(".on('command:*')", () => {110 test('when no arguments then listener not called', () => {111 const mockAction = jest.fn();112 const program = new commander.Command();113 program114 .on('command:*', mockAction);115 program.parse(['node', 'test']);116 expect(mockAction).not.toHaveBeenCalled();117 });118119 test('when unrecognised argument then listener called', () => {120 const mockAction = jest.fn();121 const program = new commander.Command();122 program123 .on('command:*', mockAction);124 program.parse(['node', 'test', 'unrecognised-command']);125 expect(mockAction).toHaveBeenCalled();126 });127128 test('when recognised command then listener not called', () => {129 const mockAction = jest.fn();130 const program = new commander.Command();131 program132 .command('install')133 .action(() => { });134 program135 .on('command:*', mockAction);136 program.parse(['node', 'test', 'install']);137 expect(mockAction).not.toHaveBeenCalled();138 });139140 test('when unrecognised command/argument then listener called', () => {141 const mockAction = jest.fn();142 const program = new commander.Command();143 program144 .command('install');145 program146 .on('command:*', mockAction);147 program.parse(['node', 'test', 'unrecognised-command']);Skip unknown options check if there is a better error to display (#1464)John Geebefore adoption4f78587
lines 148–161why, one step back148 expect(mockAction).toHaveBeenCalled();149 });150151 test('when unrecognised command/argument and unknown option then listener called', () => {152 // Give listener a chance to make a suggestion for misspelled command. The option153 // could only be unknown because the command is not correct.154 // Regression identified in https://github.com/tj/commander.js/issues/1460#issuecomment-772313494155 const mockAction = jest.fn();156 const program = new commander.Command();157 program158 .command('install');159 program160 .on('command:*', mockAction);161 program.parse(['node', 'test', 'intsall', '--unknown']);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 162–164why, one step back162 expect(mockAction).toHaveBeenCalled();163 });164});