Skip to content
Myr

command.help.test.js

This file

Path

tests/command.help.test.js

116 lines · 3.9 KiB · JavaScript

Last changed by

Add opts to addCommand, and rename noHelp to hidden (#1232)John Geebefore adoptionb5d95ee

History of this file

Take it

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 1–16why, one step back
1const commander = require('../');
23// Testing various help incantations.4//5// Note there are also specific help tests included in many of the feature tests,6// such as the alias, version, usage, name, helpOption, and commandHelp tests.78// Avoid doing many full format tests as will be broken by any layout changes!9test('when call helpInformation for program then help format is as expected (usage, options, commands)', () => {10 const program = new commander.Command();11 program12 .command('my-command <file>');13 const expectedHelpInformation =14`Usage: test [options] [command]1516Options:

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

line 17why, one step back
17 -h, --help display help for command

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 18–20why, one step back
18
19Commands:20 my-command <file>

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

line 21why, one step back
21 help [command] display help for command

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 22–23why, one step back
22`;
23

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

line 24why, one step back
24 program.name('test');

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 25–33why, one step back
25 const helpInformation = program.helpInformation();
26 expect(helpInformation).toBe(expectedHelpInformation);27});2829test('when use .description for command then help incudes description', () => {30 const program = new commander.Command();31 program32 .command('simple-command')33 .description('custom-description');

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

line 34why, one step back
34 program._help = 'test';

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 35–43why, one step back
35 const helpInformation = program.helpInformation();
36 expect(helpInformation).toMatch(/simple-command +custom-description/);37});3839test('when call .help then exit', () => {40 // Optional. Suppress normal output to keep test output clean.41 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });42 const program = new commander.Command();43 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 44why, one step back
44 .exitOverride();

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 45–55why, one step back
45 expect(() => {
46 program.help();47 }).toThrow('(outputHelp)');48 writeSpy.mockClear();49});5051test('when specify --help then exit', () => {52 // Optional. Suppress normal output to keep test output clean.53 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });54 const program = new commander.Command();55 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 56why, one step back
56 .exitOverride();

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 57–68why, one step back
57 expect(() => {
58 program.parse(['node', 'test', '--help']);59 }).toThrow('(outputHelp)');60 writeSpy.mockClear();61});6263test('when call help(cb) then display cb output and exit', () => {64 // Using spy to detect custom output65 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });66 const helpReplacement = 'reformatted help';67 const program = new commander.Command();68 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 69why, one step back
69 .exitOverride();

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 70–90why, one step back
70 expect(() => {
71 program.help((helpInformation) => {72 return helpReplacement;73 });74 }).toThrow('(outputHelp)');75 expect(writeSpy).toHaveBeenCalledWith(helpReplacement);76 writeSpy.mockClear();77});7879test('when call outputHelp(cb) then display cb output', () => {80 // Using spy to detect custom output81 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });82 const helpReplacement = 'reformatted help';83 const program = new commander.Command();84 program.outputHelp((helpInformation) => {85 return helpReplacement;86 });87 expect(writeSpy).toHaveBeenCalledWith(helpReplacement);88 writeSpy.mockClear();89});90

Add opts to addCommand, and rename noHelp to hidden (#1232)John Geebefore adoptionb5d95ee

line 91why, one step back
91// noHelp is now named hidden, not officially deprecated yet

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 92–95why, one step back
92test('when command sets noHelp then not displayed in helpInformation', () => {
93 const program = new commander.Command();94 program95 .command('secret', 'secret description', { noHelp: true });

Add opts to addCommand, and rename noHelp to hidden (#1232)John Geebefore adoptionb5d95ee

lines 96–113why, one step back
96 const helpInformation = program.helpInformation();
97 expect(helpInformation).not.toMatch('secret');98});99100test('when command sets hidden then not displayed in helpInformation', () => {101 const program = new commander.Command();102 program103 .command('secret', 'secret description', { hidden: true });104 const helpInformation = program.helpInformation();105 expect(helpInformation).not.toMatch('secret');106});107108test('when addCommand with hidden:true then not displayed in helpInformation', () => {109 const secretCmd = new commander.Command('secret');110111 const program = new commander.Command();112 program113 .addCommand(secretCmd, { hidden: true });

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 114–116why, one step back
114 const helpInformation = program.helpInformation();
115 expect(helpInformation).not.toMatch('secret');116});