Skip to content
Myr

command.unknownOption.test.js

This file

Path

tests/command.unknownOption.test.js

113 lines · 3.0 KiB · JavaScript

Last changed by

Suggestion for unknown command and unknown option (#1590)John Geebefore adoption91ccfd5

History of this file

Take it

The walk stopped after 200 commits: lines older than 733047f point at it.

Add parse result test (#1188)John Geebefore adoption733047f

lines 1–6why, one step back
1const commander = require('../');
23// Checking for detection of unknown options, including regression tests for some past issues.45describe('unknownOption', () => {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;

Add parse result test (#1188)John Geebefore adoption733047f

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(() => { });

Add parse result test (#1188)John Geebefore adoption733047f

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

configure output (#1387)John Geebefore adoptioned7f13e

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

Add parse result test (#1188)John Geebefore adoption733047f

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

configure output (#1387)John Geebefore adoptioned7f13e

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

Add parse result test (#1188)John Geebefore adoption733047f

lines 19–29why, 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 {

Missing subcommand in action for test (#1462)John Geebefore adoptionb040db4

line 30why, one step back
30 program.parse(['node', 'test', 'info', '--NONSENSE']);

Add parse result test (#1188)John Geebefore adoption733047f

lines 31–56why, one step back
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()

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 57why, one step back
57 .argument('[file]')

Add parse result test (#1188)John Geebefore adoption733047f

lines 58–73why, one step back
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()

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 74why, one step back
74 .argument('[file]')

Add parse result test (#1188)John Geebefore adoption733047f

lines 75–84why, one step back
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 });

Skip unknown options check if there is a better error to display (#1464)John Geebefore adoption4f78587

lines 85–97why, one step back
85
86 test('when specify unknown option with simple program then error', () => {87 const program = new commander.Command();88 program89 .exitOverride();90 let caughtErr;91 try {92 program.parse(['node', 'test', '--NONSENSE']);93 } catch (err) {94 caughtErr = err;95 }96 expect(caughtErr.code).toBe('commander.unknownOption');97 });

Suggestion for unknown command and unknown option (#1590)John Geebefore adoption91ccfd5

lines 98–112why, one step back
98
99 test('when specify unknown global option before subcommand then error', () => {100 const program = new commander.Command();101 program102 .exitOverride();103 program.command('sub');104105 let caughtErr;106 try {107 program.parse(['--NONSENSE', 'sub'], { from: 'user' });108 } catch (err) {109 caughtErr = err;110 }111 expect(caughtErr.code).toBe('commander.unknownOption');112 });

Add parse result test (#1188)John Geebefore adoption733047f

line 113why, one step back
113});