Skip to content
Myr

command.unknownOption.test.js

This file

Path

tests/command.unknownOption.test.js

85 lines · 2.2 KiB · JavaScript

Last changed by

configure output (#1387)John Geebefore adoptioned7f13e

History of this file

Take it

Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656

lines 1–4why, one step back
1const commander = require('../');
23// Checking for detection of unknown options, including regression tests for some past issues.4

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

line 5why, one step back
5describe('unknownOption', () => {

Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656

line 6why, one step back
6 // Optional. Use internal knowledge to suppress output to keep test output clean.

configure output (#1387)John Geebefore adoptioned7f13e

line 7why, one step back
7 let writeErrorSpy;

Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656

lines 8–9why, one step back
8
9 beforeAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 10why, one step back
10 writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });

Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656

lines 11–13why, one step back
11 });
1213 afterEach(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 14why, one step back
14 writeErrorSpy.mockClear();

Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656

lines 15–17why, one step back
15 });
1617 afterAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 18why, one step back
18 writeErrorSpy.mockRestore();

Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656

lines 19–85why, one step back
19 });
2021 test('when specify unknown option with subcommand and action handler then error', () => {22 const program = new commander.Command();23 program24 .exitOverride()25 .command('info')26 .action(() => {});2728 let caughtErr;29 try {30 program.parse(['node', 'info', '--NONSENSE']);31 } catch (err) {32 caughtErr = err;33 }34 expect(caughtErr.code).toBe('commander.unknownOption');35 });3637 test('when specify unknown option with subcommand argument and action handler then error', () => {38 const program = new commander.Command();39 program40 .exitOverride()41 .command('info <file>')42 .action(() => {});4344 let caughtErr;45 try {46 program.parse(['node', 'test', 'info', 'a', '--NONSENSE']);47 } catch (err) {48 caughtErr = err;49 }50 expect(caughtErr.code).toBe('commander.unknownOption');51 });5253 test('when specify unknown option with program and action handler then error', () => {54 const program = new commander.Command();55 program56 .exitOverride()57 .arguments('[file]')58 .action(() => {});5960 let caughtErr;61 try {62 program.parse(['node', 'test', '--NONSENSE']);63 } catch (err) {64 caughtErr = err;65 }66 expect(caughtErr.code).toBe('commander.unknownOption');67 });6869 test('when specify unknown option with program argument and action handler then error', () => {70 // Regression test from #96571 const program = new commander.Command();72 program73 .exitOverride()74 .arguments('[file]')75 .action(() => {});7677 let caughtErr;78 try {79 program.parse(['node', 'test', 'info', 'a', '--NONSENSE']);80 } catch (err) {81 caughtErr = err;82 }83 expect(caughtErr.code).toBe('commander.unknownOption');84 });85});