Skip to content
Myr

command.action.test.js

This file

Path

tests/command.action.test.js

166 lines · 6.1 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–7why, one step back
5
6// Test some behaviours of .action not covered in more specific tests.7

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

lines 8–17why, one step back
8describe('Command.action()', () => {
9 test('when .action called then command passed to action', (t) => {10 const actionMock = t.mock.fn();11 const program = new commander.Command();12 const cmd = program.command('info').action(actionMock);13 program.parse(['node', 'test', 'info']);14 const callArgs = actionMock.mock.calls[0].arguments;15 assert.equal(callArgs[0], cmd.opts());16 assert.equal(callArgs[1], cmd);17 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 18why, one step back
18

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

lines 19–26why, one step back
19 test('when .action called then this is set to command', () => {
20 const program = new commander.Command();21 let actionThis;22 const cmd = program.command('info').action(function () {23 actionThis = this;24 });25 program.parse(['node', 'test', 'info']);26 assert.equal(actionThis, cmd);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 27why, one step back
27 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 28why, one step back
28

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

lines 29–36why, one step back
29 test('when .action called then program.args only contains args', () => {
30 // At one time program.args was being modified to contain the same args as the call to .action31 // and so included the command as an extra and unexpected complex item in array.32 const program = new commander.Command();33 program.command('info <file>').action(() => {});34 program.parse(['node', 'test', 'info', 'my-file']);35 assert.deepEqual(program.args, ['info', 'my-file']);36 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 37why, one step back
37

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

lines 38–48why, one step back
38 describe('when .action on program with required argument and argument supplied then action called', () => {
39 getTestCases('<file>').forEach(([methodName, program]) => {40 test(`via ${methodName}`, (t) => {41 const actionMock = t.mock.fn();42 program.action(actionMock);43 program.parse(['node', 'test', 'my-file']);44 const callArgs = actionMock.mock.calls[0].arguments;45 assert.equal(callArgs[0], 'my-file');46 assert.equal(callArgs[1], program.opts());47 assert.equal(callArgs[2], program);48 });

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

lines 49–50why, one step back
49 });
50 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 51why, one step back
51

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

lines 52–60why, one step back
52 describe('when .action on program with required argument and argument not supplied then action not called', () => {
53 getTestCases('<file>').forEach(([methodName, program]) => {54 test(`via ${methodName}`, (t) => {55 const actionMock = t.mock.fn();56 program.action(actionMock);57 assert.throws(() => {58 program.parse(['node', 'test']);59 });60 assert.equal(actionMock.mock.callCount(), 0);

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

lines 61–63why, one step back
61 });
62 });63 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 64why, one step back
64

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

lines 65–74why, one step back
65 // Changes made in #729 to call program action handler
66 test('when .action on program and no arguments then action called', (t) => {67 const actionMock = t.mock.fn();68 const program = new commander.Command();69 program.action(actionMock);70 program.parse(['node', 'test']);71 const callArgs = actionMock.mock.calls[0].arguments;72 assert.equal(callArgs[0], program.opts());73 assert.equal(callArgs[1], program);74 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 75why, one step back
75

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

lines 76–86why, one step back
76 describe('when .action on program with optional argument supplied then action called', () => {
77 getTestCases('[file]').forEach(([methodName, program]) => {78 test(`via ${methodName}`, (t) => {79 const actionMock = t.mock.fn();80 program.action(actionMock);81 program.parse(['node', 'test', 'my-file']);82 const callArgs = actionMock.mock.calls[0].arguments;83 assert.equal(callArgs[0], 'my-file');84 assert.equal(callArgs[1], program.opts());85 assert.equal(callArgs[2], program);86 });

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

lines 87–88why, one step back
87 });
88 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 89why, one step back
89

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

lines 90–100why, one step back
90 describe('when .action on program without optional argument supplied then action called', () => {
91 getTestCases('[file]').forEach(([methodName, program]) => {92 test(`via ${methodName}`, (t) => {93 const actionMock = t.mock.fn();94 program.action(actionMock);95 program.parse(['node', 'test']);96 const callArgs = actionMock.mock.calls[0].arguments;97 assert.equal(callArgs[0], undefined);98 assert.equal(callArgs[1], program.opts());99 assert.equal(callArgs[2], program);100 });

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

lines 101–102why, one step back
101 });
102 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 103why, one step back
103

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

lines 104–109why, one step back
104 describe('when .action on program with optional argument and subcommand and program argument then program action called', () => {
105 getTestCases('[file]').forEach(([methodName, program]) => {106 test(`via ${methodName}`, (t) => {107 const actionMock = t.mock.fn();108 program.action(actionMock);109 program.command('subcommand');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 110why, one step back
110

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

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

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 112why, one step back
112

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

lines 113–117why, one step back
113 const callArgs = actionMock.mock.calls[0].arguments;
114 assert.equal(callArgs[0], 'a');115 assert.equal(callArgs[1], program.opts());116 assert.equal(callArgs[2], program);117 });

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

lines 118–119why, one step back
118 });
119 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 120why, one step back
120

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

lines 121–127why, one step back
121 // Changes made in #1062 to allow this case
122 describe('when .action on program with optional argument and subcommand and no program argument then program action called', () => {123 getTestCases('[file]').forEach(([methodName, program]) => {124 test(`via ${methodName}`, (t) => {125 const actionMock = t.mock.fn();126 program.action(actionMock);127 program.command('subcommand');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 128why, one step back
128

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

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

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 130why, one step back
130

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

lines 131–135why, one step back
131 const callArgs = actionMock.mock.calls[0].arguments;
132 assert.equal(callArgs[0], undefined);133 assert.equal(callArgs[1], program.opts());134 assert.equal(callArgs[2], program);135 });

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

lines 136–137why, one step back
136 });
137 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 138why, one step back
138

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

lines 139–146why, one step back
139 test('when action is async then can await parseAsync', async () => {
140 let asyncFinished = false;141 async function delay() {142 await new Promise((resolve) => setTimeout(resolve, 100));143 asyncFinished = true;144 }145 const program = new commander.Command();146 program.action(delay);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 147why, one step back
147

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

lines 148–152why, one step back
148 const later = program.parseAsync(['node', 'test']);
149 assert.equal(asyncFinished, false);150 await later;151 assert.equal(asyncFinished, true);152 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 153why, one step back
153

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

lines 154–166why, one step back
154 function getTestCases(arg) {
155 const withArguments = createTestCommand().arguments(arg);156 const withArgument = createTestCommand().argument(arg);157 const withAddArgument = createTestCommand().addArgument(158 new commander.Argument(arg),159 );160 return [161 ['.arguments', withArguments],162 ['.argument', withArgument],163 ['.addArgument', withAddArgument],164 ];165 }166});