Skip to content
Myr

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 file

Take 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 back
1import * 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 back
5
6// .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.15

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 16why, one step back
16describe("Command.command('*')", () => {

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 17–20why, one step back
17 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 back
21 try {
22 program.parse(['node', 'test']);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 23–25why, one step back
23 } catch (err) {
24 /* empty */25 }

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 26why, one step back
26 assert.equal(mockAction.mock.callCount(), 0);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 27–28why, one step back
27 });
28

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 29–30why, one step back
29 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 back
31 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 32why, one step back
32 program.command('*').argument('[args...]').action(mockAction);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 33why, one step back
33 program.parse(['node', 'test', 'unrecognised-command']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 34why, one step back
34 assert.equal(mockAction.mock.callCount(), 1);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 35–36why, one step back
35 });
36

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 37–38why, one step back
37 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 back
39 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 40–41why, one step back
40 program.command('install').action(() => {});
41 program.command('*').action(mockAction);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 42why, one step back
42 program.parse(['node', 'test', 'install']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 43why, one step back
43 assert.equal(mockAction.mock.callCount(), 0);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 44–45why, one step back
44 });
45

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 46–47why, one step back
46 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 back
48 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 49–50why, one step back
49 program.command('install');
50 program.command('*').argument('[args...]').action(mockAction);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 51why, one step back
51 program.parse(['node', 'test', 'unrecognised-command']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 52why, one step back
52 assert.equal(mockAction.mock.callCount(), 1);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 53–54why, one step back
53 });
54

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 55why, one step back
55 test('when unrecognised argument and known option then asterisk action called', (t) => {

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 56why, one step back
56 // 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 back
57 const mockAction = t.mock.fn();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 58why, one step back
58 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 59why, one step back
59 program.command('install');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 60–65why, one step back
60 const star = program
61 .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 back
66 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 back
68 });
69

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 70why, one step back
70 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 back
71 // This is a change in behaviour from v2 which did not error, but is consistent with modern better detection of invalid options

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 72–74why, one step back
72 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 back
75 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 back
76 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 back
84 });
85});8687// Test .on explicitly rather than assuming covered by .command

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 88why, one step back
88describe("Command.on('command:*')", () => {

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 89–90why, one step back
89 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 back
91 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 92why, one step back
92 program.on('command:*', mockAction);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 93why, one step back
93 program.parse(['node', 'test']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 94why, one step back
94 assert.equal(mockAction.mock.callCount(), 0);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 95–96why, one step back
95 });
96

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 97–98why, one step back
97 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 back
99 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 100why, one step back
100 program.on('command:*', mockAction);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 101why, one step back
101 program.parse(['node', 'test', 'unrecognised-command']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 102why, one step back
102 assert.equal(mockAction.mock.callCount(), 1);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 103–104why, one step back
103 });
104

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 105–106why, one step back
105 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 back
107 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 108–109why, one step back
108 program.command('install').action(() => {});
109 program.on('command:*', mockAction);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 110why, one step back
110 program.parse(['node', 'test', 'install']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 111why, one step back
111 assert.equal(mockAction.mock.callCount(), 0);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 112–113why, one step back
112 });
113

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 114–115why, one step back
114 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 back
116 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 117–118why, one step back
117 program.command('install');
118 program.on('command:*', mockAction);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 119why, one step back
119 program.parse(['node', 'test', 'unrecognised-command']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 120why, one step back
120 assert.equal(mockAction.mock.callCount(), 1);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 121–122why, one step back
121 });
122

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 123why, one step back
123 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 back
124 // Give listener a chance to make a suggestion for misspelled command. The option
125 // could only be unknown because the command is not correct.126 // Regression identified in https://github.com/tj/commander.js/issues/1460#issuecomment-772313494

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 127why, one step back
127 const mockAction = t.mock.fn();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 128why, one step back
128 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 129–130why, one step back
129 program.command('install');
130 program.on('command:*', mockAction);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 131why, one step back
131 program.parse(['node', 'test', 'intsall', '--unknown']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 132why, one step back
132 assert.equal(mockAction.mock.callCount(), 1);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 133–134why, one step back
133 });
134});