Skip to content
Myr

command.helpCommand.test.js

This file

Path

tests/command.helpCommand.test.js

100 lines · 3.4 KiB · JavaScript

Last changed by

configure output (#1387)John Geebefore adoptioned7f13e

History of this file

Take it

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

lines 1–33why, one step back
1const commander = require('../');
23describe('help command listed in helpInformation', () => {4 test('when program has no subcommands then no automatic help command', () => {5 const program = new commander.Command();6 const helpInformation = program.helpInformation();7 expect(helpInformation).not.toMatch(/help \[command\]/);8 });910 test('when program has no subcommands and add help command then has help command', () => {11 const program = new commander.Command();12 program.addHelpCommand(true);13 const helpInformation = program.helpInformation();14 expect(helpInformation).toMatch(/help \[command\]/);15 });1617 test('when program has subcommands then has automatic help command', () => {18 const program = new commander.Command();19 program.command('foo');20 const helpInformation = program.helpInformation();21 expect(helpInformation).toMatch(/help \[command\]/);22 });2324 test('when program has subcommands and suppress help command then no help command', () => {25 const program = new commander.Command();26 program.addHelpCommand(false);27 program.command('foo');28 const helpInformation = program.helpInformation();29 expect(helpInformation).not.toMatch(/help \[command\]/);30 });3132 test('when add custom help command then custom help command', () => {33 const program = new commander.Command();

Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912

line 34why, one step back
34 program.addHelpCommand('myHelp', 'help description');

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

line 35why, one step back
35 const helpInformation = program.helpInformation();

Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912

line 36why, one step back
36 expect(helpInformation).toMatch(/myHelp +help description/);

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

lines 37–41why, one step back
37 });
38});3940describe('help command processed on correct command', () => {41 // Use internal knowledge to suppress output to keep test output clean.

configure output (#1387)John Geebefore adoptioned7f13e

line 42why, one step back
42 let writeErrorSpy;

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

lines 43–45why, one step back
43 let writeSpy;
4445 beforeAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

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

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

lines 47–50why, one step back
47 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
48 });4950 afterEach(() => {

configure output (#1387)John Geebefore adoptioned7f13e

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

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

lines 52–55why, one step back
52 writeSpy.mockClear();
53 });5455 afterAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

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

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

lines 57–100why, one step back
57 writeSpy.mockRestore();
58 });5960 test('when "program help" then program', () => {61 const program = new commander.Command();62 program.exitOverride();63 program.command('sub1');64 program.exitOverride(() => { throw new Error('program'); });65 expect(() => {66 program.parse('node test.js help'.split(' '));67 }).toThrow('program');68 });6970 test('when "program help sub1" then sub1', () => {71 const program = new commander.Command();72 program.exitOverride();73 const sub1 = program.command('sub1');74 sub1.exitOverride(() => { throw new Error('sub1'); });75 expect(() => {76 program.parse('node test.js help sub1'.split(' '));77 }).toThrow('sub1');78 });7980 test('when "program sub1 help sub2" then sub2', () => {81 const program = new commander.Command();82 program.exitOverride();83 const sub1 = program.command('sub1');84 const sub2 = sub1.command('sub2');85 sub2.exitOverride(() => { throw new Error('sub2'); });86 expect(() => {87 program.parse('node test.js sub1 help sub2'.split(' '));88 }).toThrow('sub2');89 });9091 test('when default command and "program help" then program', () => {92 const program = new commander.Command();93 program.exitOverride();94 program.command('sub1', { isDefault: true });95 program.exitOverride(() => { throw new Error('program'); });96 expect(() => {97 program.parse('node test.js help'.split(' '));98 }).toThrow('program');99 });100});