command.unknownCommand.test.js
This file
Path
tests/command.unknownCommand.test.js
81 lines · 2.2 KiB · JavaScript
Last changed by
Add showHelpAfterError (#1534)John Geebefore adoption082717f
History of this fileTake it
The walk stopped after 200 commits: lines older than 08ec04e point at it.
Type commands property (#1184)Dmitrii Maganovbefore adoption08ec04e
lines 1–2why, one step back1const commander = require('../');2Skip unknown options check if there is a better error to display (#1464)John Geebefore adoption4f78587
line 3why, one step back3describe('unknownCommand', () => {Type commands property (#1184)Dmitrii Maganovbefore adoption08ec04e
line 4why, one step back4 // Optional. Use internal knowledge to suppress output to keep test output clean.configure output (#1387)John Geebefore adoptioned7f13e
line 5why, one step back5 let writeErrorSpy;Type commands property (#1184)Dmitrii Maganovbefore adoption08ec04e
lines 6–7why, one step back67 beforeAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 8why, one step back8 writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });Type commands property (#1184)Dmitrii Maganovbefore adoption08ec04e
lines 9–11why, one step back9 });1011 afterEach(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 12why, one step back12 writeErrorSpy.mockClear();Type commands property (#1184)Dmitrii Maganovbefore adoption08ec04e
lines 13–15why, one step back13 });1415 afterAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 16why, one step back16 writeErrorSpy.mockRestore();Type commands property (#1184)Dmitrii Maganovbefore adoption08ec04e
lines 17–26why, one step back17 });1819 test('when unknown argument in simple program then no error', () => {20 const program = new commander.Command();21 program.exitOverride();22 expect(() => {23 program.parse('node test.js unknown'.split(' '));24 }).not.toThrow();25 });26Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 27why, one step back27 test('when unknown command but action handler taking arg then no error', () => {Type commands property (#1184)Dmitrii Maganovbefore adoption08ec04e
lines 28–32why, one step back28 const program = new commander.Command();29 program30 .exitOverride()31 .command('sub');32 programMore consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 33why, one step back33 .argument('[args...]')Type commands property (#1184)Dmitrii Maganovbefore adoption08ec04e
lines 34–59why, one step back34 .action(() => { });35 expect(() => {36 program.parse('node test.js unknown'.split(' '));37 }).not.toThrow();38 });3940 test('when unknown command but listener then no error', () => {41 const program = new commander.Command();42 program43 .exitOverride()44 .command('sub');45 program46 .on('command:*', () => { });47 expect(() => {48 program.parse('node test.js unknown'.split(' '));49 }).not.toThrow();50 });5152 test('when unknown command then error', () => {53 const program = new commander.Command();54 program55 .exitOverride()56 .command('sub');57 let caughtErr;58 try {59 program.parse('node test.js unknown'.split(' '));Skip unknown options check if there is a better error to display (#1464)John Geebefore adoption4f78587
lines 60–75why, one step back60 } catch (err) {61 caughtErr = err;62 }63 expect(caughtErr.code).toBe('commander.unknownCommand');64 });6566 test('when unknown command and unknown option then error is for unknown command', () => {67 // The unknown command is more useful since the option is for an unknown command (and might be68 // ok if the command had been correctly spelled, say).69 const program = new commander.Command();70 program71 .exitOverride()72 .command('sub');73 let caughtErr;74 try {75 program.parse('node test.js sbu --silly'.split(' '));Type commands property (#1184)Dmitrii Maganovbefore adoption08ec04e
lines 76–81why, one step back76 } catch (err) {77 caughtErr = err;78 }79 expect(caughtErr.code).toBe('commander.unknownCommand');80 });81});