command.exitOverride.test.js
This file
Path
tests/command.exitOverride.test.js
303 lines · 9.0 KiB · JavaScript
Last changed by
Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–5why, one step back1const commander = require('../');2const path = require('path');34// Test details of the exitOverride errors.5// The important checks are the exitCode and code which are intended to be stable forMerge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b
line 6why, one step back6// semver minor versions. For now, also testing the error.message and that output occurredMove to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 7–8why, one step back7// to detect accidental changes in behaviour.8Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7
lines 9–10why, one step back9/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectCommanderError"] }] */10Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 11–19why, one step back11function expectCommanderError(err, exitCode, code, message) {12 expect(err).toBeInstanceOf(commander.CommanderError);13 expect(err.exitCode).toBe(exitCode);14 expect(err.code).toBe(code);15 expect(err.message).toBe(message);16}1718describe('.exitOverride and error details', () => {19 // Use internal knowledge to suppress output to keep test output clean.configure output (#1387)John Geebefore adoptioned7f13e
line 20why, one step back20 let stderrSpy;Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 21–22why, one step back2122 beforeAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 23why, one step back23 stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 24–26why, one step back24 });2526 afterEach(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 27why, one step back27 stderrSpy.mockClear();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 28–30why, one step back28 });2930 afterAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 31why, one step back31 stderrSpy.mockRestore();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 32–36why, one step back32 });3334 test('when specify unknown program option then throw CommanderError', () => {35 const program = new commander.Command();36 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 37why, one step back37 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 38–45why, one step back3839 let caughtErr;40 try {41 program.parse(['node', 'test', '-m']);42 } catch (err) {43 caughtErr = err;44 }45configure output (#1387)John Geebefore adoptioned7f13e
line 46why, one step back46 expect(stderrSpy).toHaveBeenCalled();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 47–49why, one step back47 expectCommanderError(caughtErr, 1, 'commander.unknownOption', "error: unknown option '-m'");48 });49Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 50–52why, one step back50 test('when specify unknown command then throw CommanderError', () => {51 const program = new commander.Command();52 programSuggest help when unknown command (#1179)John Geebefore adoption28b649d
line 53why, one step back53 .name('prog')Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 54–63why, one step back54 .exitOverride()55 .command('sub');5657 let caughtErr;58 try {59 program.parse(['node', 'test', 'oops']);60 } catch (err) {61 caughtErr = err;62 }63configure output (#1387)John Geebefore adoptioned7f13e
line 64why, one step back64 expect(stderrSpy).toHaveBeenCalled();Suggest help when unknown command (#1179)John Geebefore adoption28b649d
line 65why, one step back65 expectCommanderError(caughtErr, 1, 'commander.unknownCommand', "error: unknown command 'oops'. See 'prog --help'.");Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 66–67why, one step back66 });67Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 68–72why, one step back68 // Same error as above, but with custom handler.69 test('when supply custom handler then throw custom error', () => {70 const customError = new commander.CommanderError(123, 'custom-code', 'custom-message');71 const program = new commander.Command();72 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 73why, one step back73 .exitOverride((_err) => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 74–90why, one step back74 throw customError;75 });7677 let caughtErr;78 try {79 program.parse(['node', 'test', '-m']);80 } catch (err) {81 caughtErr = err;82 }8384 expectCommanderError(caughtErr, customError.exitCode, customError.code, customError.message);85 });8687 test('when specify option without required value then throw CommanderError', () => {88 const optionFlags = '-p, --pepper <type>';89 const program = new commander.Command();90 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 91why, one step back91 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 92–100why, one step back92 .option(optionFlags, 'add pepper');9394 let caughtErr;95 try {96 program.parse(['node', 'test', '--pepper']);97 } catch (err) {98 caughtErr = err;99 }100configure output (#1387)John Geebefore adoptioned7f13e
line 101why, one step back101 expect(stderrSpy).toHaveBeenCalled();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 102–107why, one step back102 expectCommanderError(caughtErr, 1, 'commander.optionMissingArgument', `error: option '${optionFlags}' argument missing`);103 });104105 test('when specify command without required argument then throw CommanderError', () => {106 const program = new commander.Command();107 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 108why, one step back108 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 109–118why, one step back109 .command('compress <arg-name>')110 .action(() => { });111112 let caughtErr;113 try {114 program.parse(['node', 'test', 'compress']);115 } catch (err) {116 caughtErr = err;117 }118configure output (#1387)John Geebefore adoptioned7f13e
line 119why, one step back119 expect(stderrSpy).toHaveBeenCalled();Merge pull request #1050 from shadowspawn/feature/update-dependenciesTomoaki Abebefore adoption36ae7a9
line 120why, one step back120 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 121–122why, one step back121 });122Add .allowExcessArguments() and error message (#1407)John Geebefore adoption09f277a
lines 123–159why, one step back123 test('when specify excess argument then throw CommanderError', () => {124 const program = new commander.Command();125 program126 .exitOverride()127 .allowExcessArguments(false)128 .action(() => { });129130 let caughtErr;131 try {132 program.parse(['node', 'test', 'excess']);133 } catch (err) {134 caughtErr = err;135 }136137 expect(stderrSpy).toHaveBeenCalled();138 expectCommanderError(caughtErr, 1, 'commander.excessArguments', 'error: too many arguments. Expected 0 arguments but got 1.');139 });140141 test('when specify command with excess argument then throw CommanderError', () => {142 const program = new commander.Command();143 program144 .exitOverride()145 .command('speak')146 .allowExcessArguments(false)147 .action(() => { });148149 let caughtErr;150 try {151 program.parse(['node', 'test', 'speak', 'excess']);152 } catch (err) {153 caughtErr = err;154 }155156 expect(stderrSpy).toHaveBeenCalled();157 expectCommanderError(caughtErr, 1, 'commander.excessArguments', "error: too many arguments for 'speak'. Expected 0 arguments but got 1.");158 });159Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
line 160why, one step back160 test('when specify --help then throw CommanderError', () => {Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 161why, one step back161 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 162–163why, one step back162 const program = new commander.Command();163 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 164why, one step back164 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 165–173why, one step back165166 let caughtErr;167 try {168 program.parse(['node', 'test', '--help']);169 } catch (err) {170 caughtErr = err;171 }172173 expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 174why, one step back174 writeSpy.mockRestore();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 175–179why, one step back175 });176177 test('when executable subcommand and no command specified then throw CommanderError', () => {178 const program = new commander.Command();179 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 180why, one step back180 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 181–189why, one step back181 .command('compress', 'compress description');182183 let caughtErr;184 try {185 program.parse(['node', 'test']);186 } catch (err) {187 caughtErr = err;188 }189Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 190why, one step back190 expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 191–193why, one step back191 });192193 test('when specify --version then throw CommanderError', () => {configure output (#1387)John Geebefore adoptioned7f13e
line 194why, one step back194 const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 195–197why, one step back195 const myVersion = '1.2.3';196 const program = new commander.Command();197 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 198why, one step back198 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 199–208why, one step back199 .version(myVersion);200201 let caughtErr;202 try {203 program.parse(['node', 'test', '--version']);204 } catch (err) {205 caughtErr = err;206 }207208 expectCommanderError(caughtErr, 0, 'commander.version', myVersion);configure output (#1387)John Geebefore adoptioned7f13e
line 209why, one step back209 stdoutSpy.mockRestore();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 210–211why, one step back210 });211tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706
lines 212–213why, one step back212 test('when executableSubcommand succeeds then call exitOverride', async() => {213 expect.hasAssertions();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 214–215why, one step back214 const pm = path.join(__dirname, 'fixtures/pm');215 const program = new commander.Command();tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706
lines 216–224why, one step back216 await new Promise((resolve) => {217 program218 .exitOverride((err) => {219 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');220 resolve();221 })222 .command('silent', 'description');223 program.parse(['node', pm, 'silent']);224 });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
line 225why, one step back225 });Add .requiredOption() for mandatory options (#1071)John Geebefore adoption7f9edba
lines 226–242why, one step back226227 test('when mandatory program option missing then throw CommanderError', () => {228 const optionFlags = '-p, --pepper <type>';229 const program = new commander.Command();230 program231 .exitOverride()232 .requiredOption(optionFlags, 'add pepper');233234 let caughtErr;235 try {236 program.parse(['node', 'test']);237 } catch (err) {238 caughtErr = err;239 }240241 expectCommanderError(caughtErr, 1, 'commander.missingMandatoryOptionValue', `error: required option '${optionFlags}' not specified`);242 });Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
lines 243–257why, one step back243244 test('when option argument not in choices then throw CommanderError', () => {245 const optionFlags = '--colour <shade>';246 const program = new commander.Command();247 program248 .exitOverride()249 .addOption(new commander.Option(optionFlags).choices(['red', 'blue']));250251 let caughtErr;252 try {253 program.parse(['--colour', 'green'], { from: 'user' });254 } catch (err) {255 caughtErr = err;256 }257Support custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0
lines 258–278why, one step back258 expectCommanderError(caughtErr, 1, 'commander.invalidOptionArgument', "error: option '--colour <shade>' argument 'green' is invalid. Allowed choices are red, blue.");259 });260261 test('when custom processing throws InvalidOptionArgumentError then throw CommanderError', () => {262 function justSayNo(value) {263 throw new commander.InvalidOptionArgumentError('NO');264 }265 const optionFlags = '--colour <shade>';266 const program = new commander.Command();267 program268 .exitOverride()269 .option(optionFlags, 'specify shade', justSayNo);270271 let caughtErr;272 try {273 program.parse(['--colour', 'green'], { from: 'user' });274 } catch (err) {275 caughtErr = err;276 }277278 expectCommanderError(caughtErr, 1, 'commander.invalidOptionArgument', "error: option '--colour <shade>' argument 'green' is invalid. NO");Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
line 279why, one step back279 });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
line 280why, one step back280});Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
lines 281–303why, one step back281282test('when no override and error then exit(1)', () => {283 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });284 const program = new commander.Command();285 program.configureOutput({ outputError: () => {} });286 program.parse(['--unknownOption'], { from: 'user' });287 expect(exitSpy).toHaveBeenCalledWith(1);288 exitSpy.mockRestore();289});290291test('when custom processing throws custom error then throw custom error', () => {292 function justSayNo(value) {293 throw new Error('custom');294 }295 const program = new commander.Command();296 program297 .exitOverride()298 .option('-s, --shade <value>', 'specify shade', justSayNo);299300 expect(() => {301 program.parse(['--shade', 'green'], { from: 'user' });302 }).toThrow('custom');303});