Skip to content
Myr

command.unknownCommand.test.js

This file

Path

tests/command.unknownCommand.test.js

81 lines · 2.1 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

lines 1–4why, one step back
1const commander = require('../');
23describe('unknownOption', () => {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–64why, 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(' '));60 } catch (err) {61 caughtErr = err;62 }63 expect(caughtErr.code).toBe('commander.unknownCommand');64 });

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

lines 65–80why, one step back
65
66 test('when unknown subcommand then help suggestion includes command path', () => {67 const program = new commander.Command();68 program69 .exitOverride()70 .command('sub')71 .command('subsub');72 let caughtErr;73 try {74 program.parse('node test.js sub unknown'.split(' '));75 } catch (err) {76 caughtErr = err;77 }78 expect(caughtErr.code).toBe('commander.unknownCommand');79 expect(writeErrorSpy.mock.calls[0][0]).toMatch('test sub');80 });

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

line 81why, one step back
81});