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 fileTake it
The walk stopped after 200 commits: lines older than 20cef03 point at it.
Update supported details for 7.x (#1419)John Geebefore adoption20cef03
lines 1–29why, one step back1const 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.7 let writeErrorSpy;89 beforeAll(() => {10 writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });11 });1213 afterEach(() => {14 writeErrorSpy.mockClear();15 });1617 afterAll(() => {18 writeErrorSpy.mockRestore();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 back30 program.parse(['node', 'test', 'info', '--NONSENSE']);Update supported details for 7.x (#1419)John Geebefore adoption20cef03
lines 31–56why, one step back31 } 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 back57 .argument('[file]')Update supported details for 7.x (#1419)John Geebefore adoption20cef03
lines 58–73why, one step back58 .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 back74 .argument('[file]')Update supported details for 7.x (#1419)John Geebefore adoption20cef03
lines 75–84why, one step back75 .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 back8586 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 back9899 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 });Update supported details for 7.x (#1419)John Geebefore adoption20cef03
line 113why, one step back113});