command.help.test.js
This file
Path
tests/command.help.test.js
255 lines · 9.0 KiB · JavaScript
Last changed by
Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
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–28why, one step back25 const helpInformation = program.helpInformation();26 expect(helpInformation).toBe(expectedHelpInformation);27});28Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b
line 29why, one step back29test('when use .description for command then help includes description', () => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 30–33why, one step back30 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–90why, 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});90Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
lines 91–97why, one step back91test('when call deprecated outputHelp(cb) with wrong callback return type then throw', () => {92 const program = new commander.Command();93 expect(() => {94 program.outputHelp((helpInformation) => 3);95 }).toThrow();96});97Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
line 98why, one step back98test('when command sets deprecated noHelp then not displayed in helpInformation', () => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 99–101why, one step back99 const program = new commander.Command();100 program101 .command('secret', 'secret description', { noHelp: true });Add opts to addCommand, and rename noHelp to hidden (#1232)John Geebefore adoptionb5d95ee
lines 102–119why, one step back102 const helpInformation = program.helpInformation();103 expect(helpInformation).not.toMatch('secret');104});105106test('when command sets hidden then not displayed in helpInformation', () => {107 const program = new commander.Command();108 program109 .command('secret', 'secret description', { hidden: true });110 const helpInformation = program.helpInformation();111 expect(helpInformation).not.toMatch('secret');112});113114test('when addCommand with hidden:true then not displayed in helpInformation', () => {115 const secretCmd = new commander.Command('secret');116117 const program = new commander.Command();118 program119 .addCommand(secretCmd, { hidden: true });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 120–122why, one step back120 const helpInformation = program.helpInformation();121 expect(helpInformation).not.toMatch('secret');122});Omit the help flags from help that are masked by other options (#1247)John Geebefore adoptione1966fc
lines 123–138why, one step back123124test('when help short flag masked then not displayed in helpInformation', () => {125 const program = new commander.Command();126 program127 .option('-h, --host', 'select host');128 const helpInformation = program.helpInformation();129 expect(helpInformation).not.toMatch(/\W-h\W.*display help/);130});131132test('when both help flags masked then not displayed in helpInformation', () => {133 const program = new commander.Command();134 program135 .option('-h, --help', 'custom');136 const helpInformation = program.helpInformation();137 expect(helpInformation).not.toMatch('display help');138});Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c
line 139why, one step back139Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
lines 140–163why, one step back140test('when call .help then output on stdout', () => {141 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });142 const program = new commander.Command();143 program144 .exitOverride();145 expect(() => {146 program.help();147 }).toThrow('(outputHelp)');148 expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());149 writeSpy.mockClear();150});151152test('when call .help with { error: true } then output on stderr', () => {153 const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });154 const program = new commander.Command();155 program156 .exitOverride();157 expect(() => {158 program.help({ error: true });159 }).toThrow('(outputHelp)');160 expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());161 writeSpy.mockClear();162});163Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
line 164why, one step back164test('when no options then Options not included in helpInformation', () => {Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c
lines 165–170why, one step back165 const program = new commander.Command();166 // No custom options, no version option, no help option167 program168 .helpOption(false);169 const helpInformation = program.helpInformation();170 expect(helpInformation).not.toMatch('Options');Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
lines 171–172why, one step back171});172Increase test coverage (#1381)John Geebefore adoption7a0baab
lines 173–181why, one step back173test('when negated option then option included in helpInformation', () => {174 const program = new commander.Command();175 program176 .option('-C, --no-colour', 'colourless');177 const helpInformation = program.helpInformation();178 expect(helpInformation).toMatch('--no-colour');179 expect(helpInformation).toMatch('colourless');180});181Include tests for supplying optional param to .hideHelp (#1372)John Geebefore adoptionb0c5884
line 182why, one step back182test('when option.hideHelp() then option not included in helpInformation', () => {Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
lines 183–187why, one step back183 const program = new commander.Command();184 program185 .addOption(new commander.Option('-s,--secret', 'secret option').hideHelp());186 const helpInformation = program.helpInformation();187 expect(helpInformation).not.toMatch('secret');Include tests for supplying optional param to .hideHelp (#1372)John Geebefore adoptionb0c5884
lines 188–203why, one step back188});189190test('when option.hideHelp(true) then option not included in helpInformation', () => {191 const program = new commander.Command();192 program193 .addOption(new commander.Option('-s,--secret', 'secret option').hideHelp(true));194 const helpInformation = program.helpInformation();195 expect(helpInformation).not.toMatch('secret');196});197198test('when option.hideHelp(false) then option included in helpInformation', () => {199 const program = new commander.Command();200 program201 .addOption(new commander.Option('-s,--secret', 'secret option').hideHelp(false));202 const helpInformation = program.helpInformation();203 expect(helpInformation).toMatch('secret');Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
lines 204–235why, one step back204});205206test('when option has default value then default included in helpInformation', () => {207 const program = new commander.Command();208 program209 .option('-p, --port <portNumber>', 'port number', 80);210 const helpInformation = program.helpInformation();211 expect(helpInformation).toMatch('(default: 80)');212});213214test('when option has default value description then default description included in helpInformation', () => {215 const program = new commander.Command();216 program217 .addOption(new commander.Option('-a, --address <dotted>', 'ip address').default('127.0.0.1', 'home'));218 const helpInformation = program.helpInformation();219 expect(helpInformation).toMatch('(default: home)');220});221222test('when option has choices then choices included in helpInformation', () => {223 const program = new commander.Command();224 program225 .addOption(new commander.Option('-c, --colour <colour>').choices(['red', 'blue']));226 const helpInformation = program.helpInformation();227 expect(helpInformation).toMatch('(choices: "red", "blue")');228});229230test('when option has choices and default then both included in helpInformation', () => {231 const program = new commander.Command();232 program233 .addOption(new commander.Option('-c, --colour <colour>').choices(['red', 'blue']).default('red'));234 const helpInformation = program.helpInformation();235 expect(helpInformation).toMatch('(choices: "red", "blue", default: "red")');Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c
line 236why, one step back236});Merge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d
lines 237–253why, one step back237238test('when arguments then included in helpInformation', () => {239 const program = new commander.Command();240 program241 .name('foo')242 .arguments('<file>');243 const helpInformation = program.helpInformation();244 expect(helpInformation).toMatch('Usage: foo [options] <file>');245});246247test('when arguments described then included in helpInformation', () => {248 const program = new commander.Command();249 program250 .arguments('<file>')251 .helpOption(false)252 .description('description', { file: 'input source' });253 const helpInformation = program.helpInformation();Merge branch 'develop' into release/7.xJohn Geebefore adoptione5512a4
line 254why, one step back254 expect(helpInformation).toMatch(/Arguments:\n +file +input source/); // [sic], extra lineMerge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d
line 255why, one step back255});