Skip to content
Myr

command.exitOverride.test.js

This file

Path

tests/command.exitOverride.test.js

209 lines · 6.1 KiB · JavaScript

Last changed by

Suggest help when unknown command (#1179)John Geebefore adoption28b649d

History of this file

Take it

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

lines 1–8why, 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.8

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 9–10why, one step back
9/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectCommanderError"] }] */
10

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

lines 11–20why, one step back
11function 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;

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

line 21why, one step back
21 let writeSpy; // used for help [sic] and version

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

lines 22–40why, one step back
22
23 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–53why, 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 });53

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 54–56why, one step back
54 test('when specify unknown command then throw CommanderError', () => {
55 const program = new commander.Command();56 program

Suggest help when unknown command (#1179)John Geebefore adoption28b649d

line 57why, one step back
57 .name('prog')

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 58–68why, one step back
58 .exitOverride()
59 .command('sub');6061 let caughtErr;62 try {63 program.parse(['node', 'test', 'oops']);64 } catch (err) {65 caughtErr = err;66 }6768 expect(consoleErrorSpy).toHaveBeenCalled();

Suggest help when unknown command (#1179)John Geebefore adoption28b649d

line 69why, one step back
69 expectCommanderError(caughtErr, 1, 'commander.unknownCommand', "error: unknown command 'oops'. See 'prog --help'.");

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 70–71why, one step back
70 });
71

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

lines 72–76why, one step back
72 // Same error as above, but with custom handler.
73 test('when supply custom handler then throw custom error', () => {74 const customError = new commander.CommanderError(123, 'custom-code', 'custom-message');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((_err) => {

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

lines 78–94why, one step back
78 throw customError;
79 });8081 let caughtErr;82 try {83 program.parse(['node', 'test', '-m']);84 } catch (err) {85 caughtErr = err;86 }8788 expectCommanderError(caughtErr, customError.exitCode, customError.code, customError.message);89 });9091 test('when specify option without required value then throw CommanderError', () => {92 const optionFlags = '-p, --pepper <type>';93 const program = new commander.Command();94 program

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

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

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

lines 96–111why, one step back
96 .option(optionFlags, 'add pepper');
9798 let caughtErr;99 try {100 program.parse(['node', 'test', '--pepper']);101 } catch (err) {102 caughtErr = err;103 }104105 expect(consoleErrorSpy).toHaveBeenCalled();106 expectCommanderError(caughtErr, 1, 'commander.optionMissingArgument', `error: option '${optionFlags}' argument missing`);107 });108109 test('when specify command without required argument 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–123why, one step back
113 .command('compress <arg-name>')
114 .action(() => { });115116 let caughtErr;117 try {118 program.parse(['node', 'test', 'compress']);119 } catch (err) {120 caughtErr = err;121 }122123 expect(consoleErrorSpy).toHaveBeenCalled();

Merge pull request #1050 from shadowspawn/feature/update-dependenciesTomoaki Abebefore adoption36ae7a9

line 124why, one step back
124 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");

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

lines 125–129why, one step back
125 });
126127 test('when specify --help then throw CommanderError', () => {128 const program = new commander.Command();129 program

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

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

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

lines 131–145why, one step back
131
132 let caughtErr;133 try {134 program.parse(['node', 'test', '--help']);135 } catch (err) {136 caughtErr = err;137 }138139 expect(writeSpy).toHaveBeenCalled();140 expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');141 });142143 test('when executable subcommand and no command specified then throw CommanderError', () => {144 const program = new commander.Command();145 program

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

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

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

lines 147–156why, one step back
147 .command('compress', 'compress description');
148149 let caughtErr;150 try {151 program.parse(['node', 'test']);152 } catch (err) {153 caughtErr = err;154 }155156 expect(writeSpy).toHaveBeenCalled();

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

line 157why, one step back
157 expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');

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

lines 158–163why, one step back
158 });
159160 test('when specify --version then throw CommanderError', () => {161 const myVersion = '1.2.3';162 const program = new commander.Command();163 program

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

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

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

lines 165–177why, one step back
165 .version(myVersion);
166167 let caughtErr;168 try {169 program.parse(['node', 'test', '--version']);170 } catch (err) {171 caughtErr = err;172 }173174 expect(writeSpy).toHaveBeenCalled();175 expectCommanderError(caughtErr, 0, 'commander.version', myVersion);176 });177

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 178–179why, one step back
178 // Have not worked out a cleaner way to do this, so suppress warning.
179 // eslint-disable-next-line jest/no-test-callback

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

lines 180–183why, one step back
180 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–191why, one step back
185 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');
186 done();187 })188 .command('silent', 'description');189190 program.parse(['node', pm, 'silent']);191 });

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

lines 192–208why, one step back
192
193 test('when mandatory program option missing then throw CommanderError', () => {194 const optionFlags = '-p, --pepper <type>';195 const program = new commander.Command();196 program197 .exitOverride()198 .requiredOption(optionFlags, 'add pepper');199200 let caughtErr;201 try {202 program.parse(['node', 'test']);203 } catch (err) {204 caughtErr = err;205 }206207 expectCommanderError(caughtErr, 1, 'commander.missingMandatoryOptionValue', `error: required option '${optionFlags}' not specified`);208 });

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

line 209why, one step back
209});