Skip to content
Myr

command.exitOverride.test.js

This file

Path

tests/command.exitOverride.test.js

230 lines · 7.0 KiB · JavaScript

Last changed by

Add .requiredOption() for mandatory options (#1071)John Geebefore adoption7f9edba

History of this file

Take it

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 1–15why, one step back
1const 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}15

Windows tests (and GitHub Actions) (#1070)John Geebefore adoption7ab5fbf

lines 16–17why, one step back
16const testOrSkipOnWindows = (process.platform === 'win32') ? test.skip : test;
17

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 18–40why, one step back
18describe('.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 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 41why, one step back
41 .exitOverride();

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 42–58why, one step back
42
43 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 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 59why, one step back
59 .exitOverride((_err) => {

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 60–76why, one step back
60 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 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 77why, one step back
77 .exitOverride()

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 78–93why, one step back
78 .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 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 94why, one step back
94 .exitOverride()

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 95–105why, one step back
95 .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 back
106 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 back
107 });
108109 test('when specify --help then throw CommanderError', () => {110 const program = new commander.Command();111 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 112why, one step back
112 .exitOverride();

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 113–127why, one step back
113
114 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 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 128why, one step back
128 .exitOverride()

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 129–146why, one step back
129 .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 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 147why, one step back
147 .exitOverride()

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 148–164why, one step back
148 .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 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 165why, one step back
165 .exitOverride()

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 166–183why, one step back
166 .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 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 184why, one step back
184 .exitOverride((err) => {

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 185–192why, one step back
185 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');
186 done();187 })188 .command('silent', 'description');189190 program.parse(['node', pm, 'silent']);191 });192

Windows tests (and GitHub Actions) (#1070)John Geebefore adoption7ab5fbf

lines 193–194why, one step back
193 // Throws directly on Windows
194 testOrSkipOnWindows('when executableSubcommand fails then call exitOverride', (done) => {

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 195–207why, one step back
195 // 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 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 208why, one step back
208 .exitOverride(exitCallback)

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 209–212why, one step back
209 .command('does-not-exist', 'fail');
210211 program.parse(['node', pm, 'does-not-exist']);212 });

Add .requiredOption() for mandatory options (#1071)John Geebefore adoption7f9edba

lines 213–229why, one step back
213
214 test('when mandatory program option missing then throw CommanderError', () => {215 const optionFlags = '-p, --pepper <type>';216 const program = new commander.Command();217 program218 .exitOverride()219 .requiredOption(optionFlags, 'add pepper');220221 let caughtErr;222 try {223 program.parse(['node', 'test']);224 } catch (err) {225 caughtErr = err;226 }227228 expectCommanderError(caughtErr, 1, 'commander.missingMandatoryOptionValue', `error: required option '${optionFlags}' not specified`);229 });

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

line 230why, one step back
230});