command.unknownOption.test.js
This file
Path
tests/command.unknownOption.test.js
98 lines · 2.6 KiB · JavaScript
Last changed by
More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
History of this fileTake it
Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
lines 1–4why, one step back1const commander = require('../');23// Checking for detection of unknown options, including regression tests for some past issues.4Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 5why, one step back5describe('unknownOption', () => {Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
line 6why, one step back6 // Optional. Use internal knowledge to suppress output to keep test output clean.configure output (#1387)John Geebefore adoptioned7f13e
line 7why, one step back7 let writeErrorSpy;Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
lines 8–9why, one step back89 beforeAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 10why, one step back10 writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
lines 11–13why, one step back11 });1213 afterEach(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 14why, one step back14 writeErrorSpy.mockClear();Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
lines 15–17why, one step back15 });1617 afterAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 18why, one step back18 writeErrorSpy.mockRestore();Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
lines 19–29why, one step back19 });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']);Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
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]')Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
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]')Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
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 });Pass unknown to command:* to detect unknown options (#1049)John Geebefore adoption9593656
line 98why, one step back98});