Skip to content
Myr

command.default.test.js

This file

Path

tests/command.default.test.js

92 lines · 3.1 KiB · JavaScript

Last changed by

Add opts to addCommand, and rename noHelp to hidden (#1232)John Geebefore adoptionb5d95ee

History of this file

Take it

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 1–2why, one step back
1const childProcess = require('child_process');
2const commander = require('../');

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 3–4why, one step back
3const path = require('path');
4const util = require('util');

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

line 5why, one step back
5

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 6–7why, one step back
6const execAsync = util.promisify(childProcess.exec);
7

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 8–11why, one step back
8describe('default executable command', () => {
9 // Calling node explicitly so pm works without file suffix cross-platform.10 const pm = path.join(__dirname, './fixtures/pm');11

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 12–14why, one step back
12 test('when default subcommand and no command then call default', async() => {
13 const { stdout } = await execAsync(`node ${pm}`);14 expect(stdout).toBe('default\n');

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 15–16why, one step back
15 });
16

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 17–19why, one step back
17 test('when default subcommand and unrecognised argument then call default with argument', async() => {
18 const { stdout } = await execAsync(`node ${pm} an-argument`);19 expect(stdout).toBe("default\n[ 'an-argument' ]\n");

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 20–21why, one step back
20 });
21

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 22–24why, one step back
22 test('when default subcommand and unrecognised option then call default with option', async() => {
23 const { stdout } = await execAsync(`node ${pm} --an-option`);24 expect(stdout).toBe("default\n[ '--an-option' ]\n");

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 25–46why, one step back
25 });
26});2728describe('default action command', () => {29 function makeProgram() {30 const program = new commander.Command();31 const actionMock = jest.fn();32 program33 .command('other');34 program35 .command('default', { isDefault: true })36 .allowUnknownOption()37 .action(actionMock);38 return { program, actionMock };39 }4041 test('when default subcommand and no command then call default', () => {42 const { program, actionMock } = makeProgram();43 program.parse('node test.js'.split(' '));44 expect(actionMock).toHaveBeenCalled();45 });46

Add opts to addCommand, and rename noHelp to hidden (#1232)John Geebefore adoptionb5d95ee

line 47why, one step back
47 test('when default subcommand and unrecognised argument then call default', () => {

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 48–52why, one step back
48 const { program, actionMock } = makeProgram();
49 program.parse('node test.js an-argument'.split(' '));50 expect(actionMock).toHaveBeenCalled();51 });52

Add opts to addCommand, and rename noHelp to hidden (#1232)John Geebefore adoptionb5d95ee

lines 53–87why, one step back
53 test('when default subcommand and unrecognised option then call default', () => {
54 const { program, actionMock } = makeProgram();55 program.parse('node test.js --an-option'.split(' '));56 expect(actionMock).toHaveBeenCalled();57 });58});5960describe('default added command', () => {61 function makeProgram() {62 const actionMock = jest.fn();63 const defaultCmd = new commander.Command('default')64 .allowUnknownOption()65 .action(actionMock);6667 const program = new commander.Command();68 program69 .command('other');70 program71 .addCommand(defaultCmd, { isDefault: true });72 return { program, actionMock };73 }7475 test('when default subcommand and no command then call default', () => {76 const { program, actionMock } = makeProgram();77 program.parse('node test.js'.split(' '));78 expect(actionMock).toHaveBeenCalled();79 });8081 test('when default subcommand and unrecognised argument then call default', () => {82 const { program, actionMock } = makeProgram();83 program.parse('node test.js an-argument'.split(' '));84 expect(actionMock).toHaveBeenCalled();85 });8687 test('when default subcommand and unrecognised option then call default', () => {

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 88–92why, one step back
88 const { program, actionMock } = makeProgram();
89 program.parse('node test.js --an-option'.split(' '));90 expect(actionMock).toHaveBeenCalled();91 });92});