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

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

History of this file

Take it

The walk stopped after 200 commits: lines older than cc2db5b point at it.

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 1–69why, 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();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 70why, one step back
70 program.allowExcessArguments(false);

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 71–117why, one step back
71 let caughtErr;
72 try {73 program.parse(['surprise'], { from: 'user' });74 } catch (err) {75 caughtErr = err;76 }77 expect(caughtErr.code).toBe('commander.excessArguments');78 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);79 });8081 test('when unknown command then shows help', () => {82 const { program, writeMock } = makeProgram();83 program.command('sub1');84 let caughtErr;85 try {86 program.parse(['sub2'], { from: 'user' });87 } catch (err) {88 caughtErr = err;89 }90 expect(caughtErr.code).toBe('commander.unknownCommand');91 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);92 });9394 test('when invalid option choice then shows help', () => {95 const { program, writeMock } = makeProgram();96 program.addOption(new commander.Option('--color').choices(['red', 'blue']));97 let caughtErr;98 try {99 program.parse(['--color', 'pink'], { from: 'user' });100 } catch (err) {101 caughtErr = err;102 }103 expect(caughtErr.code).toBe('commander.invalidArgument');104 expect(writeMock).toHaveBeenLastCalledWith(`${customHelpMessage}\n`);105 });106});107108test('when showHelpAfterError() and error and then shows full help', () => {109 const writeMock = jest.fn();110 const program = new commander.Command();111 program112 .exitOverride()113 .showHelpAfterError()114 .configureOutput({ writeErr: writeMock });115116 try {117 program.parse(['--unknown-option'], { from: 'user' });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 118–120why, one step back
118 } catch (err) {
119 /* empty */120 }

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 121–122why, one step back
121 expect(writeMock).toHaveBeenLastCalledWith(program.helpInformation());
122});