command.exitOverride.test.js
This file
Path
tests/command.exitOverride.test.js
225 lines · 6.7 KiB · JavaScript
Last changed by
Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
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–36why, 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.20 let consoleErrorSpy;2122 beforeAll(() => {23 consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });24 });2526 afterEach(() => {27 consoleErrorSpy.mockClear();28 });2930 afterAll(() => {31 consoleErrorSpy.mockRestore();32 });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–49why, one step back3839 let caughtErr;40 try {41 program.parse(['node', 'test', '-m']);42 } catch (err) {43 caughtErr = err;44 }4546 expect(consoleErrorSpy).toHaveBeenCalled();47 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–64why, 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 }6364 expect(consoleErrorSpy).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–107why, 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 }100101 expect(consoleErrorSpy).toHaveBeenCalled();102 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–119why, 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 }118119 expect(consoleErrorSpy).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–123why, one step back121 });122123 test('when specify --help then throw CommanderError', () => {Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 124why, one step back124 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 125–126why, one step back125 const program = new commander.Command();126 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 127why, one step back127 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 128–136why, one step back128129 let caughtErr;130 try {131 program.parse(['node', 'test', '--help']);132 } catch (err) {133 caughtErr = err;134 }135136 expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 137why, one step back137 writeSpy.mockRestore();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 138–140why, one step back138 });139140 test('when executable subcommand and no command specified then throw CommanderError', () => {Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 141why, one step back141 const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 142–143why, one step back142 const program = new commander.Command();143 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 144why, one step back144 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 145–153why, one step back145 .command('compress', 'compress description');146147 let caughtErr;148 try {149 program.parse(['node', 'test']);150 } catch (err) {151 caughtErr = err;152 }153Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 154why, one step back154 expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 155why, one step back155 writeSpy.mockRestore();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 156–158why, one step back156 });157158 test('when specify --version then throw CommanderError', () => {Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 159why, one step back159 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 160–162why, one step back160 const myVersion = '1.2.3';161 const program = new commander.Command();162 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 163why, one step back163 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 164–173why, one step back164 .version(myVersion);165166 let caughtErr;167 try {168 program.parse(['node', 'test', '--version']);169 } catch (err) {170 caughtErr = err;171 }172173 expectCommanderError(caughtErr, 0, 'commander.version', myVersion);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–176why, one step back175 });176tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706
lines 177–178why, one step back177 test('when executableSubcommand succeeds then call exitOverride', async() => {178 expect.hasAssertions();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 179–180why, one step back179 const pm = path.join(__dirname, 'fixtures/pm');180 const program = new commander.Command();tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706
lines 181–189why, one step back181 await new Promise((resolve) => {182 program183 .exitOverride((err) => {184 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');185 resolve();186 })187 .command('silent', 'description');188 program.parse(['node', pm, 'silent']);189 });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
line 190why, one step back190 });Add .requiredOption() for mandatory options (#1071)John Geebefore adoption7f9edba
lines 191–207why, one step back191192 test('when mandatory program option missing then throw CommanderError', () => {193 const optionFlags = '-p, --pepper <type>';194 const program = new commander.Command();195 program196 .exitOverride()197 .requiredOption(optionFlags, 'add pepper');198199 let caughtErr;200 try {201 program.parse(['node', 'test']);202 } catch (err) {203 caughtErr = err;204 }205206 expectCommanderError(caughtErr, 1, 'commander.missingMandatoryOptionValue', `error: required option '${optionFlags}' not specified`);207 });Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
lines 208–224why, one step back208209 test('when option argument not in choices then throw CommanderError', () => {210 const optionFlags = '--colour <shade>';211 const program = new commander.Command();212 program213 .exitOverride()214 .addOption(new commander.Option(optionFlags).choices(['red', 'blue']));215216 let caughtErr;217 try {218 program.parse(['--colour', 'green'], { from: 'user' });219 } catch (err) {220 caughtErr = err;221 }222223 expectCommanderError(caughtErr, 1, 'commander.optionArgumentRejected', `error: option '${optionFlags}' argument of 'green' not in allowed choices: red, blue`);224 });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
line 225why, one step back225});