command.helpCommand.test.js
This file
Path
tests/command.helpCommand.test.js
100 lines · 3.4 KiB · JavaScript
Last changed by
Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
History of this fileTake it
Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 1–33why, one step back1const commander = require('../');23describe('help command listed in helpInformation', () => {4 test('when program has no subcommands then no automatic help command', () => {5 const program = new commander.Command();6 const helpInformation = program.helpInformation();7 expect(helpInformation).not.toMatch(/help \[command\]/);8 });910 test('when program has no subcommands and add help command then has help command', () => {11 const program = new commander.Command();12 program.addHelpCommand(true);13 const helpInformation = program.helpInformation();14 expect(helpInformation).toMatch(/help \[command\]/);15 });1617 test('when program has subcommands then has automatic help command', () => {18 const program = new commander.Command();19 program.command('foo');20 const helpInformation = program.helpInformation();21 expect(helpInformation).toMatch(/help \[command\]/);22 });2324 test('when program has subcommands and suppress help command then no help command', () => {25 const program = new commander.Command();26 program.addHelpCommand(false);27 program.command('foo');28 const helpInformation = program.helpInformation();29 expect(helpInformation).not.toMatch(/help \[command\]/);30 });3132 test('when add custom help command then custom help command', () => {33 const program = new commander.Command();Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
line 34why, one step back34 program.addHelpCommand('myHelp', 'help description');Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 35why, one step back35 const helpInformation = program.helpInformation();Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
line 36why, one step back36 expect(helpInformation).toMatch(/myHelp +help description/);Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 37–100why, one step back37 });38});3940describe('help command processed on correct command', () => {41 // Use internal knowledge to suppress output to keep test output clean.42 let consoleErrorSpy;43 let writeSpy;4445 beforeAll(() => {46 consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });47 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });48 });4950 afterEach(() => {51 consoleErrorSpy.mockClear();52 writeSpy.mockClear();53 });5455 afterAll(() => {56 consoleErrorSpy.mockRestore();57 writeSpy.mockRestore();58 });5960 test('when "program help" then program', () => {61 const program = new commander.Command();62 program.exitOverride();63 program.command('sub1');64 program.exitOverride(() => { throw new Error('program'); });65 expect(() => {66 program.parse('node test.js help'.split(' '));67 }).toThrow('program');68 });6970 test('when "program help sub1" then sub1', () => {71 const program = new commander.Command();72 program.exitOverride();73 const sub1 = program.command('sub1');74 sub1.exitOverride(() => { throw new Error('sub1'); });75 expect(() => {76 program.parse('node test.js help sub1'.split(' '));77 }).toThrow('sub1');78 });7980 test('when "program sub1 help sub2" then sub2', () => {81 const program = new commander.Command();82 program.exitOverride();83 const sub1 = program.command('sub1');84 const sub2 = sub1.command('sub2');85 sub2.exitOverride(() => { throw new Error('sub2'); });86 expect(() => {87 program.parse('node test.js sub1 help sub2'.split(' '));88 }).toThrow('sub2');89 });9091 test('when default command and "program help" then program', () => {92 const program = new commander.Command();93 program.exitOverride();94 program.command('sub1', { isDefault: true });95 program.exitOverride(() => { throw new Error('program'); });96 expect(() => {97 program.parse('node test.js help'.split(' '));98 }).toThrow('program');99 });100});