command.exitOverride.test.js
This file
Path
tests/command.exitOverride.test.js
213 lines · 6.4 KiB · JavaScript
Last changed by
Windows tests (and GitHub Actions) (#1070)John Geebefore adoption7ab5fbf
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–15why, 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}15Windows tests (and GitHub Actions) (#1070)John Geebefore adoption7ab5fbf
lines 16–17why, one step back16const testOrSkipOnWindows = (process.platform === 'win32') ? test.skip : test;17Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 18–40why, one step back18describe('.exitOverride and error details', () => {19 // Use internal knowledge to suppress output to keep test output clean.20 let consoleErrorSpy;21 let writeSpy;2223 beforeAll(() => {24 consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });25 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });26 });2728 afterEach(() => {29 consoleErrorSpy.mockClear();30 writeSpy.mockClear();31 });3233 afterAll(() => {34 consoleErrorSpy.mockRestore();35 writeSpy.mockRestore();36 });3738 test('when specify unknown program option then throw CommanderError', () => {39 const program = new commander.Command();40 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 41why, one step back41 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 42–58why, one step back4243 let caughtErr;44 try {45 program.parse(['node', 'test', '-m']);46 } catch (err) {47 caughtErr = err;48 }4950 expect(consoleErrorSpy).toHaveBeenCalled();51 expectCommanderError(caughtErr, 1, 'commander.unknownOption', "error: unknown option '-m'");52 });5354 // Same error as above, but with custom handler.55 test('when supply custom handler then throw custom error', () => {56 const customError = new commander.CommanderError(123, 'custom-code', 'custom-message');57 const program = new commander.Command();58 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 59why, one step back59 .exitOverride((_err) => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 60–76why, one step back60 throw customError;61 });6263 let caughtErr;64 try {65 program.parse(['node', 'test', '-m']);66 } catch (err) {67 caughtErr = err;68 }6970 expectCommanderError(caughtErr, customError.exitCode, customError.code, customError.message);71 });7273 test('when specify option without required value then throw CommanderError', () => {74 const optionFlags = '-p, --pepper <type>';75 const program = new commander.Command();76 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 77why, one step back77 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 78–93why, one step back78 .option(optionFlags, 'add pepper');7980 let caughtErr;81 try {82 program.parse(['node', 'test', '--pepper']);83 } catch (err) {84 caughtErr = err;85 }8687 expect(consoleErrorSpy).toHaveBeenCalled();88 expectCommanderError(caughtErr, 1, 'commander.optionMissingArgument', `error: option '${optionFlags}' argument missing`);89 });9091 test('when specify command without required argument then throw CommanderError', () => {92 const program = new commander.Command();93 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 94why, one step back94 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 95–105why, one step back95 .command('compress <arg-name>')96 .action(() => { });9798 let caughtErr;99 try {100 program.parse(['node', 'test', 'compress']);101 } catch (err) {102 caughtErr = err;103 }104105 expect(consoleErrorSpy).toHaveBeenCalled();Merge pull request #1050 from shadowspawn/feature/update-dependenciesTomoaki Abebefore adoption36ae7a9
line 106why, one step back106 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 107–111why, one step back107 });108109 test('when specify --help then throw CommanderError', () => {110 const program = new commander.Command();111 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 112why, one step back112 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 113–127why, one step back113114 let caughtErr;115 try {116 program.parse(['node', 'test', '--help']);117 } catch (err) {118 caughtErr = err;119 }120121 expect(writeSpy).toHaveBeenCalled();122 expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');123 });124125 test('when executable subcommand and no command specified then throw CommanderError', () => {126 const program = new commander.Command();127 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 128why, one step back128 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 129–146why, one step back129 .command('compress', 'compress description');130131 let caughtErr;132 try {133 program.parse(['node', 'test']);134 } catch (err) {135 caughtErr = err;136 }137138 // This is effectively treated as a deliberate request for help, rather than an error.139 expect(writeSpy).toHaveBeenCalled();140 expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');141 });142143 test('when specify --version then throw CommanderError', () => {144 const myVersion = '1.2.3';145 const program = new commander.Command();146 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 147why, one step back147 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 148–164why, one step back148 .version(myVersion);149150 let caughtErr;151 try {152 program.parse(['node', 'test', '--version']);153 } catch (err) {154 caughtErr = err;155 }156157 expect(writeSpy).toHaveBeenCalled();158 expectCommanderError(caughtErr, 0, 'commander.version', myVersion);159 });160161 test('when program variadic argument not last then throw CommanderError', () => {162 // Note: this error is notified during parse, although could have been detected at declaration.163 const program = new commander.Command();164 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 165why, one step back165 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 166–183why, one step back166 .arguments('<myVariadicArg...> [optionalArg]')167 .action(jest.fn);168169 let caughtErr;170 try {171 program.parse(['node', 'test', 'a']);172 } catch (err) {173 caughtErr = err;174 }175176 expect(consoleErrorSpy).toHaveBeenCalled();177 expectCommanderError(caughtErr, 1, 'commander.variadicArgNotLast', "error: variadic arguments must be last 'myVariadicArg'");178 });179180 test('when executableSubcommand succeeds then call exitOverride', (done) => {181 const pm = path.join(__dirname, 'fixtures/pm');182 const program = new commander.Command();183 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 184why, one step back184 .exitOverride((err) => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 185–192why, one step back185 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');186 done();187 })188 .command('silent', 'description');189190 program.parse(['node', pm, 'silent']);191 });192Windows tests (and GitHub Actions) (#1070)John Geebefore adoption7ab5fbf
lines 193–194why, one step back193 // Throws directly on Windows194 testOrSkipOnWindows('when executableSubcommand fails then call exitOverride', (done) => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 195–207why, one step back195 // Tricky for override, get called for `error` event then `exit` event.196 const exitCallback = jest.fn()197 .mockImplementationOnce((err) => {198 expectCommanderError(err, 1, 'commander.executeSubCommandAsync', '(error)');199 expect(err.nestedError.code).toBe('ENOENT');200 })201 .mockImplementation((err) => {202 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');203 done();204 });205 const pm = path.join(__dirname, 'fixtures/pm');206 const program = new commander.Command();207 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 208why, one step back208 .exitOverride(exitCallback)Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 209–213why, one step back209 .command('does-not-exist', 'fail');210211 program.parse(['node', pm, 'does-not-exist']);212 });213});