Skip to content
Myr

command.unknownCommand.test.js

This file

Path

tests/command.unknownCommand.test.js

97 lines · 2.7 KiB · JavaScript

Last changed by

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

History of this file

Take it

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 1–2why, one step back
1const commander = require('../');
2

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

line 3why, one step back
3describe('unknownCommand', () => {

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

line 4why, one step back
4 // Optional. Use internal knowledge to suppress output to keep test output clean.

configure output (#1387)John Geebefore adoptioned7f13e

line 5why, one step back
5 let writeErrorSpy;

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 6–7why, one step back
6
7 beforeAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 8why, one step back
8 writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

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

configure output (#1387)John Geebefore adoptioned7f13e

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

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

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

configure output (#1387)John Geebefore adoptioned7f13e

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

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 17–20why, one step back
17 });
1819 test('when unknown argument in simple program then no error', () => {20 const program = new commander.Command();

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 21–24why, one step back
21 program.exitOverride();
22 expect(() => {23 program.parse('node test.js unknown'.split(' '));24 }).not.toThrow();

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 25–26why, one step back
25 });
26

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 27why, one step back
27 test('when unknown command but action handler taking arg then no error', () => {

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 28–29why, one step back
28 const program = new commander.Command();
29 program

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 30–32why, one step back
30 .exitOverride()
31 .command('sub');32 program

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 33why, one step back
33 .arguments('[args...]')

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

line 34why, one step back
34 .action(() => { });

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 35–37why, one step back
35 expect(() => {
36 program.parse('node test.js unknown'.split(' '));37 }).not.toThrow();

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 38–42why, one step back
38 });
3940 test('when unknown command but listener then no error', () => {41 const program = new commander.Command();42 program

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 43–45why, one step back
43 .exitOverride()
44 .command('sub');45 program

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

line 46why, one step back
46 .on('command:*', () => { });

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 47–49why, one step back
47 expect(() => {
48 program.parse('node test.js unknown'.split(' '));49 }).not.toThrow();

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 50–59why, one step back
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 back
60 } 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(' '));

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 76–80why, one step back
76 } catch (err) {
77 caughtErr = err;78 }79 expect(caughtErr.code).toBe('commander.unknownCommand');80 });

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

lines 81–96why, one step back
81
82 test('when unknown subcommand then help suggestion includes command path', () => {83 const program = new commander.Command();84 program85 .exitOverride()86 .command('sub')87 .command('subsub');88 let caughtErr;89 try {90 program.parse('node test.js sub unknown'.split(' '));91 } catch (err) {92 caughtErr = err;93 }94 expect(caughtErr.code).toBe('commander.unknownCommand');95 expect(writeErrorSpy.mock.calls[0][0]).toMatch('test sub');96 });

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

line 97why, one step back
97});