Skip to content
Myr

command.default.test.js

This file

Path

tests/command.default.test.js

94 lines · 3.2 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 20cef03 point at it.

Update supported details for 7.x (#1419)John Geebefore adoption20cef03

lines 1–94why, one step back
1const childProcess = require('child_process');
2const commander = require('../');3const path = require('path');4const util = require('util');56const execFileAsync = util.promisify(childProcess.execFile);78describe('default executable command', () => {9 // Calling node explicitly so pm works without file suffix cross-platform.10 const pm = path.join(__dirname, './fixtures/pm');1112 test('when default subcommand and no command then call default', async() => {13 const { stdout } = await execFileAsync('node', [pm]);14 expect(stdout).toBe('default\n');15 });1617 test('when default subcommand and unrecognised argument then call default with argument', async() => {18 const { stdout } = await execFileAsync('node', [pm, 'an-argument']);19 expect(stdout).toBe("default\n[ 'an-argument' ]\n");20 });2122 test('when default subcommand and unrecognised option then call default with option', async() => {23 const { stdout } = await execFileAsync('node', [pm, '--an-option']);24 expect(stdout).toBe("default\n[ '--an-option' ]\n");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 .allowExcessArguments()38 .action(actionMock);39 return { program, actionMock };40 }4142 test('when default subcommand and no command then call default', () => {43 const { program, actionMock } = makeProgram();44 program.parse('node test.js'.split(' '));45 expect(actionMock).toHaveBeenCalled();46 });4748 test('when default subcommand and unrecognised argument then call default', () => {49 const { program, actionMock } = makeProgram();50 program.parse('node test.js an-argument'.split(' '));51 expect(actionMock).toHaveBeenCalled();52 });5354 test('when default subcommand and unrecognised option then call default', () => {55 const { program, actionMock } = makeProgram();56 program.parse('node test.js --an-option'.split(' '));57 expect(actionMock).toHaveBeenCalled();58 });59});6061describe('default added command', () => {62 function makeProgram() {63 const actionMock = jest.fn();64 const defaultCmd = new commander.Command('default')65 .allowUnknownOption()66 .allowExcessArguments()67 .action(actionMock);6869 const program = new commander.Command();70 program71 .command('other');72 program73 .addCommand(defaultCmd, { isDefault: true });74 return { program, actionMock };75 }7677 test('when default subcommand and no command then call default', () => {78 const { program, actionMock } = makeProgram();79 program.parse('node test.js'.split(' '));80 expect(actionMock).toHaveBeenCalled();81 });8283 test('when default subcommand and unrecognised argument then call default', () => {84 const { program, actionMock } = makeProgram();85 program.parse('node test.js an-argument'.split(' '));86 expect(actionMock).toHaveBeenCalled();87 });8889 test('when default subcommand and unrecognised option then call default', () => {90 const { program, actionMock } = makeProgram();91 program.parse('node test.js --an-option'.split(' '));92 expect(actionMock).toHaveBeenCalled();93 });94});