Skip to content
Myr

command.default.test.js

This file

Path

tests/command.default.test.js

58 lines · 2.1 KiB · JavaScript

Last changed by

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

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–58why, 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 });4647 test('when default subcommand and unrecognised argument then call default with argument', () => {48 const { program, actionMock } = makeProgram();49 program.parse('node test.js an-argument'.split(' '));50 expect(actionMock).toHaveBeenCalled();51 });5253 test('when default subcommand and unrecognised option then call default with option', () => {54 const { program, actionMock } = makeProgram();55 program.parse('node test.js --an-option'.split(' '));56 expect(actionMock).toHaveBeenCalled();57 });58});