Skip to content
Myr

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 file

Take 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 back
1const 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 back
12 expect(actionMock).toHaveBeenCalledWith(cmd.opts(), cmd);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 13–14why, one step back
13});
14

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 15–24why, one step back
15test('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});24

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 25–35why, one step back
25test('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});35

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 36why, one step back
36test.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 back
37 const actionMock = jest.fn();

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 38why, one step back
38 program.action(actionMock);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

line 39why, one step back
39 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 back
40 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 back
41});
42

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 43why, one step back
43test.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 back
44 const actionMock = jest.fn();
45 program46 .exitOverride()

configure output (#1387)John Geebefore adoptioned7f13e

line 47why, one step back
47 .configureOutput({ writeErr: () => {} })

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 48–61why, one step back
48 .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 back
62 expect(actionMock).toHaveBeenCalledWith(program.opts(), program);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 63–64why, one step back
63});
64

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 65why, one step back
65test.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 back
66 const actionMock = jest.fn();

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 67why, one step back
67 program.action(actionMock);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

line 68why, one step back
68 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 back
69 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 back
70});
71

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 72why, one step back
72test.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 back
73 const actionMock = jest.fn();

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 74why, one step back
74 program.action(actionMock);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

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

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 76why, one step back
76 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 back
77});
78

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 79why, one step back
79test.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 back
80 const actionMock = jest.fn();

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 81why, one step back
81 program.action(actionMock);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 82–86why, one step back
82 program
83 .command('subcommand');8485 program.parse(['node', 'test', 'a']);86

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 87why, one step back
87 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 back
88});
8990// Changes made in #1062 to allow this case

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 91why, one step back
91test.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 back
92 const actionMock = jest.fn();

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

lines 93–94why, one step back
93 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 back
95
96 program.parse(['node', 'test']);97

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 98why, one step back
98 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 back
99});
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 back
106 }

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 107–115why, one step back
107 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 back
116
117function 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}