command.unknownOption.test.js
This file
Path
tests/command.unknownOption.test.js
78 lines · 2.2 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import { createTestCommand } from './testHelpers.js';import { test, describe } from 'node:test';import assert from 'node:assert/strict';
// Checking for detection of unknown options, including regression tests for some past issues.
describe('parsing unknown option', () => { test('when specify unknown option with subcommand and action handler then error', () => { const program = createTestCommand(); program.command('info').action(() => {});
assert.throws( () => { program.parse(['node', 'test', 'info', '--NONSENSE']); }, { code: 'commander.unknownOption' }, ); });
test('when specify unknown option with subcommand argument and action handler then error', () => { const program = createTestCommand(); program.command('info <file>').action(() => {});
assert.throws( () => { program.parse(['node', 'test', 'info', 'a', '--NONSENSE']); }, { code: 'commander.unknownOption' }, ); });
test('when specify unknown option with program and action handler then error', () => { const program = createTestCommand(); program.argument('[file]').action(() => {});
assert.throws( () => { program.parse(['node', 'test', '--NONSENSE']); }, { code: 'commander.unknownOption' }, ); });
test('when specify unknown option with program argument and action handler then error', () => { // Regression test from #965 const program = createTestCommand(); program.argument('[file]').action(() => {});
assert.throws( () => { program.parse(['node', 'test', 'info', 'a', '--NONSENSE']); }, { code: 'commander.unknownOption' }, ); });
test('when specify unknown option with simple program then error', () => { const program = createTestCommand(); assert.throws( () => { program.parse(['node', 'test', '--NONSENSE']); }, { code: 'commander.unknownOption' }, ); });
test('when specify unknown global option before subcommand then error', () => { const program = createTestCommand(); program.command('sub');
assert.throws( () => { program.parse(['--NONSENSE', 'sub'], { from: 'user' }); }, { code: 'commander.unknownOption' }, ); });});