command.asterisk.test.js
This file
Path
tests/command.asterisk.test.js
134 lines · 5.3 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
The walk stopped after 200 commits: lines older than 471ec40 point at it.
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–4why, one step back1import * as commander from '../index.js';2import { createTestCommand } from './testHelpers.js';3import { test, describe } from 'node:test';4import assert from 'node:assert/strict';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 5–15why, one step back56// .command('*') is the old main/default command handler. It adds a listener7// for 'command:*'. It has been somewhat replaced by the program action handler,8// so most uses are probably old code. Current plan is keep the code backwards compatible9// and put work in elsewhere for new code (e.g. evolving behaviour for program action handler).10//11// The event 'command:*' is also listened for directly for testing for unknown commands12// due to an example in the README.13//14// Historical: the event 'command:*' used to also be shared by the action handler on the program.15Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 16why, one step back16describe("Command.command('*')", () => {Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 17–20why, one step back17 test('when no arguments then asterisk action not called', (t) => {18 const mockAction = t.mock.fn();19 const program = createTestCommand();20 program.command('*').action(mockAction);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 21–22why, one step back21 try {22 program.parse(['node', 'test']);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 23–25why, one step back23 } catch (err) {24 /* empty */25 }Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 26why, one step back26 assert.equal(mockAction.mock.callCount(), 0);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 27–28why, one step back27 });28Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 29–30why, one step back29 test('when unrecognised argument then asterisk action called', (t) => {30 const mockAction = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 31why, one step back31 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 32why, one step back32 program.command('*').argument('[args...]').action(mockAction);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 33why, one step back33 program.parse(['node', 'test', 'unrecognised-command']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 34why, one step back34 assert.equal(mockAction.mock.callCount(), 1);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 35–36why, one step back35 });36Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 37–38why, one step back37 test('when recognised command then asterisk action not called', (t) => {38 const mockAction = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 39why, one step back39 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 40–41why, one step back40 program.command('install').action(() => {});41 program.command('*').action(mockAction);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 42why, one step back42 program.parse(['node', 'test', 'install']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 43why, one step back43 assert.equal(mockAction.mock.callCount(), 0);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 44–45why, one step back44 });45Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 46–47why, one step back46 test('when unrecognised command/argument then asterisk action called', (t) => {47 const mockAction = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 48why, one step back48 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 49–50why, one step back49 program.command('install');50 program.command('*').argument('[args...]').action(mockAction);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 51why, one step back51 program.parse(['node', 'test', 'unrecognised-command']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 52why, one step back52 assert.equal(mockAction.mock.callCount(), 1);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 53–54why, one step back53 });54Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 55why, one step back55 test('when unrecognised argument and known option then asterisk action called', (t) => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 56why, one step back56 // This tests for a regression between v4 and v5. Known default option should not be rejected by program.Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 57why, one step back57 const mockAction = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 58why, one step back58 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 59why, one step back59 program.command('install');Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 60–65why, one step back60 const star = program61 .command('*')62 .argument('[args...]')63 .option('-d, --debug')64 .action(mockAction);65 program.parse(['node', 'test', 'unrecognised-command', '--debug']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 66–67why, one step back66 assert.equal(mockAction.mock.callCount(), 1);67 assert.equal(star.opts().debug, true);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 68–69why, one step back68 });69Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 70why, one step back70 test('when non-command argument and unknown option then error for unknown option', (t) => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 71why, one step back71 // This is a change in behaviour from v2 which did not error, but is consistent with modern better detection of invalid optionsSwitch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 72–74why, one step back72 const mockAction = t.mock.fn();73 const program = createTestCommand();74 program.command('install');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 75why, one step back75 program.command('*').argument('[args...]').action(mockAction);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 76–83why, one step back76 assert.throws(77 () => {78 program.parse(['node', 'test', 'some-argument', '--unknown']);79 },80 {81 code: 'commander.unknownOption',82 },83 );Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 84–87why, one step back84 });85});8687// Test .on explicitly rather than assuming covered by .commandAdd describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 88why, one step back88describe("Command.on('command:*')", () => {Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 89–90why, one step back89 test('when no arguments then listener not called', (t) => {90 const mockAction = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 91why, one step back91 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 92why, one step back92 program.on('command:*', mockAction);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 93why, one step back93 program.parse(['node', 'test']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 94why, one step back94 assert.equal(mockAction.mock.callCount(), 0);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 95–96why, one step back95 });96Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 97–98why, one step back97 test('when unrecognised argument then listener called', (t) => {98 const mockAction = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 99why, one step back99 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 100why, one step back100 program.on('command:*', mockAction);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 101why, one step back101 program.parse(['node', 'test', 'unrecognised-command']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 102why, one step back102 assert.equal(mockAction.mock.callCount(), 1);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 103–104why, one step back103 });104Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 105–106why, one step back105 test('when recognised command then listener not called', (t) => {106 const mockAction = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 107why, one step back107 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 108–109why, one step back108 program.command('install').action(() => {});109 program.on('command:*', mockAction);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 110why, one step back110 program.parse(['node', 'test', 'install']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 111why, one step back111 assert.equal(mockAction.mock.callCount(), 0);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 112–113why, one step back112 });113Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 114–115why, one step back114 test('when unrecognised command/argument then listener called', (t) => {115 const mockAction = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 116why, one step back116 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 117–118why, one step back117 program.command('install');118 program.on('command:*', mockAction);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 119why, one step back119 program.parse(['node', 'test', 'unrecognised-command']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 120why, one step back120 assert.equal(mockAction.mock.callCount(), 1);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 121–122why, one step back121 });122Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 123why, one step back123 test('when unrecognised command/argument and unknown option then listener called', (t) => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 124–126why, one step back124 // Give listener a chance to make a suggestion for misspelled command. The option125 // could only be unknown because the command is not correct.126 // Regression identified in https://github.com/tj/commander.js/issues/1460#issuecomment-772313494Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 127why, one step back127 const mockAction = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 128why, one step back128 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 129–130why, one step back129 program.command('install');130 program.on('command:*', mockAction);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 131why, one step back131 program.parse(['node', 'test', 'intsall', '--unknown']);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 132why, one step back132 assert.equal(mockAction.mock.callCount(), 1);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 133–134why, one step back133 });134});