command.help.test.js
This file
Path
tests/command.help.test.js
97 lines · 3.2 KiB · JavaScript
Last changed by
Parse rework for nested commands (#1149)John Geebefore adoption1345f98
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–16why, one step back1const 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 back17 -h, --help display help for commandMove to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 18–20why, one step back1819Commands:20 my-command <file>Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 21why, one step back21 help [command] display help for commandMove to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 22–23why, one step back22`;23Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 24why, one step back24 program.name('test');Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 25–33why, one step back25 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 back34 program._help = 'test';Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 35–43why, one step back35 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 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 44why, one step back44 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 45–55why, one step back45 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 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 56why, one step back56 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 57–68why, one step back57 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 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 69why, one step back69 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 70–97why, one step back70 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});9091test('when command sets noHelp then not displayed in helpInformation', () => {92 const program = new commander.Command();93 program94 .command('secret', 'secret description', { noHelp: true });95 const helpInformation = program.helpInformation();96 expect(helpInformation).not.toMatch('secret');97});