command.exitOverride.test.js
This file
Path
tests/command.exitOverride.test.js
210 lines · 6.3 KiB · JavaScript
Last changed by
Merge pull request #1050 from shadowspawn/feature/update-dependenciesTomoaki Abebefore adoption36ae7a9
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–38why, 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 for6// semver minor versions. For now, also testing the error.message and that output occured7// to detect accidental changes in behaviour.89function expectCommanderError(err, exitCode, code, message) {10 expect(err).toBeInstanceOf(commander.CommanderError);11 expect(err.exitCode).toBe(exitCode);12 expect(err.code).toBe(code);13 expect(err.message).toBe(message);14}1516describe('.exitOverride and error details', () => {17 // Use internal knowledge to suppress output to keep test output clean.18 let consoleErrorSpy;19 let writeSpy;2021 beforeAll(() => {22 consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });23 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });24 });2526 afterEach(() => {27 consoleErrorSpy.mockClear();28 writeSpy.mockClear();29 });3031 afterAll(() => {32 consoleErrorSpy.mockRestore();33 writeSpy.mockRestore();34 });3536 test('when specify unknown program option then throw CommanderError', () => {37 const program = new commander.Command();38 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 39why, one step back39 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 40–56why, one step back4041 let caughtErr;42 try {43 program.parse(['node', 'test', '-m']);44 } catch (err) {45 caughtErr = err;46 }4748 expect(consoleErrorSpy).toHaveBeenCalled();49 expectCommanderError(caughtErr, 1, 'commander.unknownOption', "error: unknown option '-m'");50 });5152 // Same error as above, but with custom handler.53 test('when supply custom handler then throw custom error', () => {54 const customError = new commander.CommanderError(123, 'custom-code', 'custom-message');55 const program = new commander.Command();56 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 57why, one step back57 .exitOverride((_err) => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 58–74why, one step back58 throw customError;59 });6061 let caughtErr;62 try {63 program.parse(['node', 'test', '-m']);64 } catch (err) {65 caughtErr = err;66 }6768 expectCommanderError(caughtErr, customError.exitCode, customError.code, customError.message);69 });7071 test('when specify option without required value then throw CommanderError', () => {72 const optionFlags = '-p, --pepper <type>';73 const program = new commander.Command();74 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 75why, one step back75 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 76–91why, one step back76 .option(optionFlags, 'add pepper');7778 let caughtErr;79 try {80 program.parse(['node', 'test', '--pepper']);81 } catch (err) {82 caughtErr = err;83 }8485 expect(consoleErrorSpy).toHaveBeenCalled();86 expectCommanderError(caughtErr, 1, 'commander.optionMissingArgument', `error: option '${optionFlags}' argument missing`);87 });8889 test('when specify command without required argument then throw CommanderError', () => {90 const program = new commander.Command();91 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 92why, one step back92 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 93–103why, one step back93 .command('compress <arg-name>')94 .action(() => { });9596 let caughtErr;97 try {98 program.parse(['node', 'test', 'compress']);99 } catch (err) {100 caughtErr = err;101 }102103 expect(consoleErrorSpy).toHaveBeenCalled();Merge pull request #1050 from shadowspawn/feature/update-dependenciesTomoaki Abebefore adoption36ae7a9
line 104why, one step back104 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 105–109why, one step back105 });106107 test('when specify --help then throw CommanderError', () => {108 const program = new commander.Command();109 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 110why, one step back110 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 111–125why, one step back111112 let caughtErr;113 try {114 program.parse(['node', 'test', '--help']);115 } catch (err) {116 caughtErr = err;117 }118119 expect(writeSpy).toHaveBeenCalled();120 expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');121 });122123 test('when executable subcommand and no command specified then throw CommanderError', () => {124 const program = new commander.Command();125 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 126why, one step back126 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 127–144why, one step back127 .command('compress', 'compress description');128129 let caughtErr;130 try {131 program.parse(['node', 'test']);132 } catch (err) {133 caughtErr = err;134 }135136 // This is effectively treated as a deliberate request for help, rather than an error.137 expect(writeSpy).toHaveBeenCalled();138 expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');139 });140141 test('when specify --version then throw CommanderError', () => {142 const myVersion = '1.2.3';143 const program = new commander.Command();144 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 145why, one step back145 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 146–162why, one step back146 .version(myVersion);147148 let caughtErr;149 try {150 program.parse(['node', 'test', '--version']);151 } catch (err) {152 caughtErr = err;153 }154155 expect(writeSpy).toHaveBeenCalled();156 expectCommanderError(caughtErr, 0, 'commander.version', myVersion);157 });158159 test('when program variadic argument not last then throw CommanderError', () => {160 // Note: this error is notified during parse, although could have been detected at declaration.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–181why, one step back164 .arguments('<myVariadicArg...> [optionalArg]')165 .action(jest.fn);166167 let caughtErr;168 try {169 program.parse(['node', 'test', 'a']);170 } catch (err) {171 caughtErr = err;172 }173174 expect(consoleErrorSpy).toHaveBeenCalled();175 expectCommanderError(caughtErr, 1, 'commander.variadicArgNotLast', "error: variadic arguments must be last 'myVariadicArg'");176 });177178 test('when executableSubcommand succeeds then call exitOverride', (done) => {179 const pm = path.join(__dirname, 'fixtures/pm');180 const program = new commander.Command();181 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 182why, one step back182 .exitOverride((err) => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 183–204why, one step back183 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');184 done();185 })186 .command('silent', 'description');187188 program.parse(['node', pm, 'silent']);189 });190191 test('when executableSubcommand fails then call exitOverride', (done) => {192 // Tricky for override, get called for `error` event then `exit` event.193 const exitCallback = jest.fn()194 .mockImplementationOnce((err) => {195 expectCommanderError(err, 1, 'commander.executeSubCommandAsync', '(error)');196 expect(err.nestedError.code).toBe('ENOENT');197 })198 .mockImplementation((err) => {199 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');200 done();201 });202 const pm = path.join(__dirname, 'fixtures/pm');203 const program = new commander.Command();204 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 205why, one step back205 .exitOverride(exitCallback)Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 206–210why, one step back206 .command('does-not-exist', 'fail');207208 program.parse(['node', pm, 'does-not-exist']);209 });210});