command.showHelpAfterError.test.js
This file
Path
tests/command.showHelpAfterError.test.js
139 lines · 4.2 KiB · JavaScript
Last changed by
Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140const commander = require('../');const { test, describe, mock } = require('node:test');const assert = require('node:assert/strict');
describe('Command.showHelpAfterError(message)', () => { const customHelpMessage = 'See --help';
function makeProgram(t) { const writeMock = t.mock.fn(); const program = new commander.Command(); program .exitOverride() .showHelpAfterError(customHelpMessage) .configureOutput({ writeErr: writeMock });
return { program, writeMock }; }
test('when missing command-argument then shows help', (t) => { const { program, writeMock } = makeProgram(t); program.argument('<file>'); assert.throws( () => { program.parse([], { from: 'user' }); }, { code: 'commander.missingArgument', }, ); const lastCall = writeMock.mock.calls[writeMock.mock.calls.length - 1]; assert.equal(lastCall.arguments[0], `${customHelpMessage}\n`); });
test('when missing option-argument then shows help', (t) => { const { program, writeMock } = makeProgram(t); program.option('--output <file>'); assert.throws( () => { program.parse(['--output'], { from: 'user' }); }, { code: 'commander.optionMissingArgument', }, ); const lastCall = writeMock.mock.calls[writeMock.mock.calls.length - 1]; assert.equal(lastCall.arguments[0], `${customHelpMessage}\n`); });
test('when missing mandatory option then shows help', (t) => { const { program, writeMock } = makeProgram(t); program.requiredOption('--password <cipher>'); assert.throws( () => { program.parse([], { from: 'user' }); }, { code: 'commander.missingMandatoryOptionValue', }, ); const lastCall = writeMock.mock.calls[writeMock.mock.calls.length - 1]; assert.equal(lastCall.arguments[0], `${customHelpMessage}\n`); });
test('when unknown option then shows help', (t) => { const { program, writeMock } = makeProgram(t); assert.throws( () => { program.parse(['--unknown-option'], { from: 'user' }); }, { code: 'commander.unknownOption', }, ); const lastCall = writeMock.mock.calls[writeMock.mock.calls.length - 1]; assert.equal(lastCall.arguments[0], `${customHelpMessage}\n`); });
test('when too many command-arguments then shows help', (t) => { const { program, writeMock } = makeProgram(t); program.allowExcessArguments(false); assert.throws( () => { program.parse(['surprise'], { from: 'user' }); }, { code: 'commander.excessArguments', }, ); const lastCall = writeMock.mock.calls[writeMock.mock.calls.length - 1]; assert.equal(lastCall.arguments[0], `${customHelpMessage}\n`); });
test('when unknown command then shows help', (t) => { const { program, writeMock } = makeProgram(t); program.command('sub1'); assert.throws( () => { program.parse(['sub2'], { from: 'user' }); }, { code: 'commander.unknownCommand', }, ); const lastCall = writeMock.mock.calls[writeMock.mock.calls.length - 1]; assert.equal(lastCall.arguments[0], `${customHelpMessage}\n`); });
test('when invalid option choice then shows help', (t) => { const { program, writeMock } = makeProgram(t); program.addOption(new commander.Option('--color').choices(['red', 'blue'])); assert.throws( () => { program.parse(['--color', 'pink'], { from: 'user' }); }, { code: 'commander.invalidArgument', }, ); const lastCall = writeMock.mock.calls[writeMock.mock.calls.length - 1]; assert.equal(lastCall.arguments[0], `${customHelpMessage}\n`); });});
test('when Command.showHelpAfterError() and error and then shows full help', () => { const writeMock = mock.fn(); const program = new commander.Command(); program .exitOverride() .showHelpAfterError() .configureOutput({ writeErr: writeMock });
try { program.parse(['--unknown-option'], { from: 'user' }); } catch (err) { /* empty */ } const lastCall = writeMock.mock.calls[writeMock.mock.calls.length - 1]; assert.equal(lastCall.arguments[0], program.helpInformation());});