command.unknownCommand.test.js
This file
Path
tests/command.unknownCommand.test.js
72 lines · 2.1 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
The walk stopped after 200 commits: lines older than 411ca95 point at it.
Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 1–7why, one step back1const commander = require('../');23describe('unknownCommand', () => {4 // Optional. Use internal knowledge to suppress output to keep test output clean.5 let writeErrorSpy;67 beforeAll(() => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 8–10why, one step back8 writeErrorSpy = jest9 .spyOn(process.stderr, 'write')10 .mockImplementation(() => {});Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 11–30why, one step back11 });1213 afterEach(() => {14 writeErrorSpy.mockClear();15 });1617 afterAll(() => {18 writeErrorSpy.mockRestore();19 });2021 test('when unknown argument in simple program then no error', () => {22 const program = new commander.Command();23 program.exitOverride();24 expect(() => {25 program.parse('node test.js unknown'.split(' '));26 }).not.toThrow();27 });2829 test('when unknown command but action handler taking arg then no error', () => {30 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 31–32why, one step back31 program.exitOverride().command('sub');32 program.argument('[args...]').action(() => {});Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 33–39why, one step back33 expect(() => {34 program.parse('node test.js unknown'.split(' '));35 }).not.toThrow();36 });3738 test('when unknown command but listener then no error', () => {39 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 40–41why, one step back40 program.exitOverride().command('sub');41 program.on('command:*', () => {});Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 42–48why, one step back42 expect(() => {43 program.parse('node test.js unknown'.split(' '));44 }).not.toThrow();45 });4647 test('when unknown command then error', () => {48 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 49why, one step back49 program.exitOverride().command('sub');Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 50–62why, one step back50 let caughtErr;51 try {52 program.parse('node test.js unknown'.split(' '));53 } catch (err) {54 caughtErr = err;55 }56 expect(caughtErr.code).toBe('commander.unknownCommand');57 });5859 test('when unknown command and unknown option then error is for unknown command', () => {60 // The unknown command is more useful since the option is for an unknown command (and might be61 // ok if the command had been correctly spelled, say).62 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 63why, one step back63 program.exitOverride().command('sub');Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 64–72why, one step back64 let caughtErr;65 try {66 program.parse('node test.js sbu --silly'.split(' '));67 } catch (err) {68 caughtErr = err;69 }70 expect(caughtErr.code).toBe('commander.unknownCommand');71 });72});