command.helpCommand.test.js
This file
Path
tests/command.helpCommand.test.js
126 lines · 4.2 KiB · JavaScript
Last changed by
Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
History of this fileTake it
The walk stopped after 200 commits: lines older than 733047f point at it.
Add parse result test (#1188)John Geebefore adoption733047f
lines 1–23why, 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 });23Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
lines 24–39why, one step back24 test('when program has subcommands and specify only unknown option then display help', () => {25 const program = new commander.Command();26 program27 .configureHelp({ formatHelp: () => '' })28 .exitOverride()29 .allowUnknownOption()30 .command('foo');31 let caughtErr;32 try {33 program.parse(['--unknown'], { from: 'user' });34 } catch (err) {35 caughtErr = err;36 }37 expect(caughtErr.code).toEqual('commander.help');38 });39Add parse result test (#1188)John Geebefore adoption733047f
lines 40–49why, one step back40 test('when program has subcommands and suppress help command then no help command', () => {41 const program = new commander.Command();42 program.addHelpCommand(false);43 program.command('foo');44 const helpInformation = program.helpInformation();45 expect(helpInformation).not.toMatch(/help \[command\]/);46 });4748 test('when add custom help command then custom help command', () => {49 const program = new commander.Command();Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
line 50why, one step back50 program.addHelpCommand('myHelp', 'help description');Add parse result test (#1188)John Geebefore adoption733047f
line 51why, one step back51 const helpInformation = program.helpInformation();Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912
line 52why, one step back52 expect(helpInformation).toMatch(/myHelp +help description/);Add parse result test (#1188)John Geebefore adoption733047f
lines 53–57why, one step back53 });54});5556describe('help command processed on correct command', () => {57 // Use internal knowledge to suppress output to keep test output clean.configure output (#1387)John Geebefore adoptioned7f13e
line 58why, one step back58 let writeErrorSpy;Add parse result test (#1188)John Geebefore adoption733047f
lines 59–61why, one step back59 let writeSpy;6061 beforeAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 62why, one step back62 writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });Add parse result test (#1188)John Geebefore adoption733047f
lines 63–66why, one step back63 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });64 });6566 afterEach(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 67why, one step back67 writeErrorSpy.mockClear();Add parse result test (#1188)John Geebefore adoption733047f
lines 68–71why, one step back68 writeSpy.mockClear();69 });7071 afterAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 72why, one step back72 writeErrorSpy.mockRestore();Add parse result test (#1188)John Geebefore adoption733047f
lines 73–82why, one step back73 writeSpy.mockRestore();74 });7576 test('when "program help" then program', () => {77 const program = new commander.Command();78 program.exitOverride();79 program.command('sub1');80 program.exitOverride(() => { throw new Error('program'); });81 expect(() => {82 program.parse('node test.js help'.split(' '));Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
lines 83–92why, one step back83 }).toThrow('program');84 });8586 test('when "program help unknown" then program', () => {87 const program = new commander.Command();88 program.exitOverride();89 program.command('sub1');90 program.exitOverride(() => { throw new Error('program'); });91 expect(() => {92 program.parse('node test.js help unknown'.split(' '));Add parse result test (#1188)John Geebefore adoption733047f
lines 93–126why, one step back93 }).toThrow('program');94 });9596 test('when "program help sub1" then sub1', () => {97 const program = new commander.Command();98 program.exitOverride();99 const sub1 = program.command('sub1');100 sub1.exitOverride(() => { throw new Error('sub1'); });101 expect(() => {102 program.parse('node test.js help sub1'.split(' '));103 }).toThrow('sub1');104 });105106 test('when "program sub1 help sub2" then sub2', () => {107 const program = new commander.Command();108 program.exitOverride();109 const sub1 = program.command('sub1');110 const sub2 = sub1.command('sub2');111 sub2.exitOverride(() => { throw new Error('sub2'); });112 expect(() => {113 program.parse('node test.js sub1 help sub2'.split(' '));114 }).toThrow('sub2');115 });116117 test('when default command and "program help" then program', () => {118 const program = new commander.Command();119 program.exitOverride();120 program.command('sub1', { isDefault: true });121 program.exitOverride(() => { throw new Error('program'); });122 expect(() => {123 program.parse('node test.js help'.split(' '));124 }).toThrow('program');125 });126});