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 fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697const commander = require('../');
// Testing various help incantations.//// Note there are also specific help tests included in many of the feature tests,// such as the alias, version, usage, name, helpOption, and commandHelp tests.
// Avoid doing many full format tests as will be broken by any layout changes!test('when call helpInformation for program then help format is as expected (usage, options, commands)', () => { const program = new commander.Command(); program .command('my-command <file>'); const expectedHelpInformation =`Usage: test [options] [command]
Options: -h, --help output usage information
Commands: my-command <file>`;
program.parse(['node', 'test']); const helpInformation = program.helpInformation(); expect(helpInformation).toBe(expectedHelpInformation);});
test('when use .description for command then help incudes description', () => { const program = new commander.Command(); program .command('simple-command') .description('custom-description'); program.parse(['node', 'test']); const helpInformation = program.helpInformation(); expect(helpInformation).toMatch(/simple-command +custom-description/);});
test('when call .help then exit', () => { // Optional. Suppress normal output to keep test output clean. const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); const program = new commander.Command(); program .exitOverride(); expect(() => { program.help(); }).toThrow('(outputHelp)'); writeSpy.mockClear();});
test('when specify --help then exit', () => { // Optional. Suppress normal output to keep test output clean. const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); const program = new commander.Command(); program .exitOverride(); expect(() => { program.parse(['node', 'test', '--help']); }).toThrow('(outputHelp)'); writeSpy.mockClear();});
test('when call help(cb) then display cb output and exit', () => { // Using spy to detect custom output const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); const helpReplacement = 'reformatted help'; const program = new commander.Command(); program .exitOverride(); expect(() => { program.help((helpInformation) => { return helpReplacement; }); }).toThrow('(outputHelp)'); expect(writeSpy).toHaveBeenCalledWith(helpReplacement); writeSpy.mockClear();});
test('when call outputHelp(cb) then display cb output', () => { // Using spy to detect custom output const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); const helpReplacement = 'reformatted help'; const program = new commander.Command(); program.outputHelp((helpInformation) => { return helpReplacement; }); expect(writeSpy).toHaveBeenCalledWith(helpReplacement); writeSpy.mockClear();});
test('when command sets noHelp then not displayed in helpInformation', () => { const program = new commander.Command(); program .command('secret', 'secret description', { noHelp: true }); const helpInformation = program.helpInformation(); expect(helpInformation).not.toMatch('secret');});