Skip to content
Myr

command.help.test.js

This file

Path

tests/command.help.test.js

96 lines · 3.1 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

lines 1–42why, 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:17 -h, --help output usage information1819Commands:20 my-command <file>21`;2223 program.parse(['node', 'test']);24 const helpInformation = program.helpInformation();25 expect(helpInformation).toBe(expectedHelpInformation);26});2728test('when use .description for command then help incudes description', () => {29 const program = new commander.Command();30 program31 .command('simple-command')32 .description('custom-description');33 program.parse(['node', 'test']);34 const helpInformation = program.helpInformation();35 expect(helpInformation).toMatch(/simple-command +custom-description/);36});3738test('when call .help then exit', () => {39 // Optional. Suppress normal output to keep test output clean.40 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });41 const program = new commander.Command();42 program

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

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

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

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

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

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

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

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

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

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

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

lines 69–96why, one step back
69 expect(() => {
70 program.help((helpInformation) => {71 return helpReplacement;72 });73 }).toThrow('(outputHelp)');74 expect(writeSpy).toHaveBeenCalledWith(helpReplacement);75 writeSpy.mockClear();76});7778test('when call outputHelp(cb) then display cb output', () => {79 // Using spy to detect custom output80 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });81 const helpReplacement = 'reformatted help';82 const program = new commander.Command();83 program.outputHelp((helpInformation) => {84 return helpReplacement;85 });86 expect(writeSpy).toHaveBeenCalledWith(helpReplacement);87 writeSpy.mockClear();88});8990test('when command sets noHelp then not displayed in helpInformation', () => {91 const program = new commander.Command();92 program93 .command('secret', 'secret description', { noHelp: true });94 const helpInformation = program.helpInformation();95 expect(helpInformation).not.toMatch('secret');96});