Skip to content
Myr

command.showHelpAfterError.test.js

This file

Path

tests/command.showHelpAfterError.test.js

122 lines · 3.7 KiB · JavaScript

Last changed by

Add showHelpAfterError (#1534)John Geebefore adoption082717f

History of this file

Take it

Add showHelpAfterError (#1534)John Geebefore adoption082717f

lines 1–122why, one step back
1const commander = require('../');
23describe('showHelpAfterError with message', () => {4 const customHelpMessage = 'See --help';56 function makeProgram() {7 const writeMock = jest.fn();8 const program = new commander.Command();9 program10 .exitOverride()11 .showHelpAfterError(customHelpMessage)12 .configureOutput({ writeErr: writeMock });1314 return { program, writeMock };15 }1617 test('when missing command-argument then shows help', () => {18 const { program, writeMock } = makeProgram();19 program.argument('<file>');20 let caughtErr;21 try {22 program.parse([], { from: 'user' });23 } catch (err) {24 caughtErr = err;25 }26 expect(caughtErr.code).toBe('commander.missingArgument');27 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);28 });2930 test('when missing option-argument then shows help', () => {31 const { program, writeMock } = makeProgram();32 program.option('--output <file>');33 let caughtErr;34 try {35 program.parse(['--output'], { from: 'user' });36 } catch (err) {37 caughtErr = err;38 }39 expect(caughtErr.code).toBe('commander.optionMissingArgument');40 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);41 });4243 test('when missing mandatory option then shows help', () => {44 const { program, writeMock } = makeProgram();45 program.requiredOption('--password <cipher>');46 let caughtErr;47 try {48 program.parse([], { from: 'user' });49 } catch (err) {50 caughtErr = err;51 }52 expect(caughtErr.code).toBe('commander.missingMandatoryOptionValue');53 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);54 });5556 test('when unknown option then shows help', () => {57 const { program, writeMock } = makeProgram();58 let caughtErr;59 try {60 program.parse(['--unknown-option'], { from: 'user' });61 } catch (err) {62 caughtErr = err;63 }64 expect(caughtErr.code).toBe('commander.unknownOption');65 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);66 });6768 test('when too many command-arguments then shows help', () => {69 const { program, writeMock } = makeProgram();70 program71 .allowExcessArguments(false);72 let caughtErr;73 try {74 program.parse(['surprise'], { from: 'user' });75 } catch (err) {76 caughtErr = err;77 }78 expect(caughtErr.code).toBe('commander.excessArguments');79 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);80 });8182 test('when unknown command then shows help', () => {83 const { program, writeMock } = makeProgram();84 program.command('sub1');85 let caughtErr;86 try {87 program.parse(['sub2'], { from: 'user' });88 } catch (err) {89 caughtErr = err;90 }91 expect(caughtErr.code).toBe('commander.unknownCommand');92 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);93 });9495 test('when invalid option choice then shows help', () => {96 const { program, writeMock } = makeProgram();97 program.addOption(new commander.Option('--color').choices(['red', 'blue']));98 let caughtErr;99 try {100 program.parse(['--color', 'pink'], { from: 'user' });101 } catch (err) {102 caughtErr = err;103 }104 expect(caughtErr.code).toBe('commander.invalidArgument');105 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);106 });107});108109test('when showHelpAfterError() and error and then shows full help', () => {110 const writeMock = jest.fn();111 const program = new commander.Command();112 program113 .exitOverride()114 .showHelpAfterError()115 .configureOutput({ writeErr: writeMock });116117 try {118 program.parse(['--unknown-option'], { from: 'user' });119 } catch (err) {120 }121 expect(writeMock).toHaveBeenLastCalledWith(program.helpInformation());122});