command.default.test.js
This file
Path
tests/command.default.test.js
93 lines · 3.3 KiB · JavaScript
Last changed by
Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394const childProcess = require('child_process');const commander = require('../');const path = require('path');const util = require('util');const { test, describe } = require('node:test');const assert = require('node:assert/strict');
const execFileAsync = util.promisify(childProcess.execFile);
describe('default executable command', () => { // Calling node explicitly so pm works without file suffix cross-platform. const pm = path.join(__dirname, './fixtures/pm');
test('when default subcommand and no command then call default', async () => { const { stdout } = await execFileAsync('node', [pm]); assert.equal(stdout, 'default\n'); });
test('when default subcommand and unrecognised argument then call default with argument', async () => { const { stdout } = await execFileAsync('node', [pm, 'an-argument']); assert.equal(stdout, "default\n[ 'an-argument' ]\n"); });
test('when default subcommand and unrecognised option then call default with option', async () => { const { stdout } = await execFileAsync('node', [pm, '--an-option']); assert.equal(stdout, "default\n[ '--an-option' ]\n"); });});
describe('default action command', () => { function makeProgram(t) { const program = new commander.Command(); const actionMock = t.mock.fn(); program.command('other'); program .command('default', { isDefault: true }) .allowUnknownOption() .allowExcessArguments() .action(actionMock); return { program, actionMock }; }
test('when default subcommand and no command then call default', (t) => { const { program, actionMock } = makeProgram(t); program.parse('node test.js'.split(' ')); assert.equal(actionMock.mock.callCount(), 1); });
test('when default subcommand and unrecognised argument then call default', (t) => { const { program, actionMock } = makeProgram(t); program.parse('node test.js an-argument'.split(' ')); assert.equal(actionMock.mock.callCount(), 1); });
test('when default subcommand and unrecognised option then call default', (t) => { const { program, actionMock } = makeProgram(t); program.parse('node test.js --an-option'.split(' ')); assert.equal(actionMock.mock.callCount(), 1); });});
describe('default added command', () => { function makeProgram(t) { const actionMock = t.mock.fn(); const defaultCmd = new commander.Command('default') .allowUnknownOption() .allowExcessArguments() .action(actionMock);
const program = new commander.Command(); program.command('other'); program.addCommand(defaultCmd, { isDefault: true }); return { program, actionMock }; }
test('when default subcommand and no command then call default', (t) => { const { program, actionMock } = makeProgram(t); program.parse('node test.js'.split(' ')); assert.equal(actionMock.mock.callCount(), 1); });
test('when default subcommand and unrecognised argument then call default', (t) => { const { program, actionMock } = makeProgram(t); program.parse('node test.js an-argument'.split(' ')); assert.equal(actionMock.mock.callCount(), 1); });
test('when default subcommand and unrecognised option then call default', (t) => { const { program, actionMock } = makeProgram(t); program.parse('node test.js --an-option'.split(' ')); assert.equal(actionMock.mock.callCount(), 1); });});