Skip to content
Myr

command.exitOverride.test.js

This file

Path

tests/command.exitOverride.test.js

303 lines · 9.0 KiB · JavaScript

Last changed by

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

History of this file

Take it

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

lines 1–5why, 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 for

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

line 6why, one step back
6// semver minor versions. For now, also testing the error.message and that output occurred

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

lines 7–8why, one step back
7// 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–19why, 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.

configure output (#1387)John Geebefore adoptioned7f13e

line 20why, one step back
20 let stderrSpy;

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

lines 21–22why, one step back
21
22 beforeAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 23why, one step back
23 stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });

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

lines 24–26why, one step back
24 });
2526 afterEach(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 27why, one step back
27 stderrSpy.mockClear();

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

lines 28–30why, one step back
28 });
2930 afterAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 31why, one step back
31 stderrSpy.mockRestore();

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

lines 32–36why, one step back
32 });
3334 test('when specify unknown program option then throw CommanderError', () => {35 const program = new commander.Command();36 program

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

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

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

lines 38–45why, one step back
38
39 let caughtErr;40 try {41 program.parse(['node', 'test', '-m']);42 } catch (err) {43 caughtErr = err;44 }45

configure output (#1387)John Geebefore adoptioned7f13e

line 46why, one step back
46 expect(stderrSpy).toHaveBeenCalled();

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

lines 47–49why, one step back
47 expectCommanderError(caughtErr, 1, 'commander.unknownOption', "error: unknown option '-m'");
48 });49

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

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

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

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

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

lines 54–63why, one step back
54 .exitOverride()
55 .command('sub');5657 let caughtErr;58 try {59 program.parse(['node', 'test', 'oops']);60 } catch (err) {61 caughtErr = err;62 }63

configure output (#1387)John Geebefore adoptioned7f13e

line 64why, one step back
64 expect(stderrSpy).toHaveBeenCalled();

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

line 65why, one step back
65 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 back
66 });
67

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

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

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

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

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

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

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

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

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

lines 92–100why, one step back
92 .option(optionFlags, 'add pepper');
9394 let caughtErr;95 try {96 program.parse(['node', 'test', '--pepper']);97 } catch (err) {98 caughtErr = err;99 }100

configure output (#1387)John Geebefore adoptioned7f13e

line 101why, one step back
101 expect(stderrSpy).toHaveBeenCalled();

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

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

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

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

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

lines 109–118why, one step back
109 .command('compress <arg-name>')
110 .action(() => { });111112 let caughtErr;113 try {114 program.parse(['node', 'test', 'compress']);115 } catch (err) {116 caughtErr = err;117 }118

configure output (#1387)John Geebefore adoptioned7f13e

line 119why, one step back
119 expect(stderrSpy).toHaveBeenCalled();

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

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

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

lines 121–122why, one step back
121 });
122

Add .allowExcessArguments() and error message (#1407)John Geebefore adoption09f277a

lines 123–159why, one step back
123 test('when specify excess argument then throw CommanderError', () => {
124 const program = new commander.Command();125 program126 .exitOverride()127 .allowExcessArguments(false)128 .action(() => { });129130 let caughtErr;131 try {132 program.parse(['node', 'test', 'excess']);133 } catch (err) {134 caughtErr = err;135 }136137 expect(stderrSpy).toHaveBeenCalled();138 expectCommanderError(caughtErr, 1, 'commander.excessArguments', 'error: too many arguments. Expected 0 arguments but got 1.');139 });140141 test('when specify command with excess argument then throw CommanderError', () => {142 const program = new commander.Command();143 program144 .exitOverride()145 .command('speak')146 .allowExcessArguments(false)147 .action(() => { });148149 let caughtErr;150 try {151 program.parse(['node', 'test', 'speak', 'excess']);152 } catch (err) {153 caughtErr = err;154 }155156 expect(stderrSpy).toHaveBeenCalled();157 expectCommanderError(caughtErr, 1, 'commander.excessArguments', "error: too many arguments for 'speak'. Expected 0 arguments but got 1.");158 });159

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

line 160why, one step back
160 test('when specify --help then throw CommanderError', () => {

Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d

line 161why, one step back
161 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });

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

lines 162–163why, one step back
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–173why, one step back
165
166 let caughtErr;167 try {168 program.parse(['node', 'test', '--help']);169 } catch (err) {170 caughtErr = err;171 }172173 expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');

Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d

line 174why, one step back
174 writeSpy.mockRestore();

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

lines 175–179why, one step back
175 });
176177 test('when executable subcommand and no command specified then throw CommanderError', () => {178 const program = new commander.Command();179 program

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

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

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

lines 181–189why, one step back
181 .command('compress', 'compress description');
182183 let caughtErr;184 try {185 program.parse(['node', 'test']);186 } catch (err) {187 caughtErr = err;188 }189

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

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

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

lines 191–193why, one step back
191 });
192193 test('when specify --version then throw CommanderError', () => {

configure output (#1387)John Geebefore adoptioned7f13e

line 194why, one step back
194 const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });

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

lines 195–197why, one step back
195 const myVersion = '1.2.3';
196 const program = new commander.Command();197 program

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

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

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

lines 199–208why, one step back
199 .version(myVersion);
200201 let caughtErr;202 try {203 program.parse(['node', 'test', '--version']);204 } catch (err) {205 caughtErr = err;206 }207208 expectCommanderError(caughtErr, 0, 'commander.version', myVersion);

configure output (#1387)John Geebefore adoptioned7f13e

line 209why, one step back
209 stdoutSpy.mockRestore();

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

lines 210–211why, one step back
210 });
211

tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706

lines 212–213why, one step back
212 test('when executableSubcommand succeeds then call exitOverride', async() => {
213 expect.hasAssertions();

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

lines 214–215why, one step back
214 const pm = path.join(__dirname, 'fixtures/pm');
215 const program = new commander.Command();

tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706

lines 216–224why, one step back
216 await new Promise((resolve) => {
217 program218 .exitOverride((err) => {219 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');220 resolve();221 })222 .command('silent', 'description');223 program.parse(['node', pm, 'silent']);224 });

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

line 225why, one step back
225 });

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

lines 226–242why, one step back
226
227 test('when mandatory program option missing then throw CommanderError', () => {228 const optionFlags = '-p, --pepper <type>';229 const program = new commander.Command();230 program231 .exitOverride()232 .requiredOption(optionFlags, 'add pepper');233234 let caughtErr;235 try {236 program.parse(['node', 'test']);237 } catch (err) {238 caughtErr = err;239 }240241 expectCommanderError(caughtErr, 1, 'commander.missingMandatoryOptionValue', `error: required option '${optionFlags}' not specified`);242 });

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc

lines 243–257why, one step back
243
244 test('when option argument not in choices then throw CommanderError', () => {245 const optionFlags = '--colour <shade>';246 const program = new commander.Command();247 program248 .exitOverride()249 .addOption(new commander.Option(optionFlags).choices(['red', 'blue']));250251 let caughtErr;252 try {253 program.parse(['--colour', 'green'], { from: 'user' });254 } catch (err) {255 caughtErr = err;256 }257

Support custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0

lines 258–278why, one step back
258 expectCommanderError(caughtErr, 1, 'commander.invalidOptionArgument', "error: option '--colour <shade>' argument 'green' is invalid. Allowed choices are red, blue.");
259 });260261 test('when custom processing throws InvalidOptionArgumentError then throw CommanderError', () => {262 function justSayNo(value) {263 throw new commander.InvalidOptionArgumentError('NO');264 }265 const optionFlags = '--colour <shade>';266 const program = new commander.Command();267 program268 .exitOverride()269 .option(optionFlags, 'specify shade', justSayNo);270271 let caughtErr;272 try {273 program.parse(['--colour', 'green'], { from: 'user' });274 } catch (err) {275 caughtErr = err;276 }277278 expectCommanderError(caughtErr, 1, 'commander.invalidOptionArgument', "error: option '--colour <shade>' argument 'green' is invalid. NO");

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc

line 279why, one step back
279 });

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

line 280why, one step back
280});

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

lines 281–303why, one step back
281
282test('when no override and error then exit(1)', () => {283 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });284 const program = new commander.Command();285 program.configureOutput({ outputError: () => {} });286 program.parse(['--unknownOption'], { from: 'user' });287 expect(exitSpy).toHaveBeenCalledWith(1);288 exitSpy.mockRestore();289});290291test('when custom processing throws custom error then throw custom error', () => {292 function justSayNo(value) {293 throw new Error('custom');294 }295 const program = new commander.Command();296 program297 .exitOverride()298 .option('-s, --shade <value>', 'specify shade', justSayNo);299300 expect(() => {301 program.parse(['--shade', 'green'], { from: 'user' });302 }).toThrow('custom');303});