command.exitOverride.test.js
This file
Path
tests/command.exitOverride.test.js
209 lines · 6.1 KiB · JavaScript
Last changed by
tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–5why, 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 forMerge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b
line 6why, one step back6// semver minor versions. For now, also testing the error.message and that output occurredMove to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 7–8why, one step back7// to detect accidental changes in behaviour.8Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7
lines 9–10why, one step back9/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectCommanderError"] }] */10Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 11–20why, one step back11function 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 back21 let writeSpy; // used for help [sic] and versionMove to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 22–40why, one step back2223 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–53why, 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 });53Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 54–56why, one step back54 test('when specify unknown command then throw CommanderError', () => {55 const program = new commander.Command();56 programSuggest help when unknown command (#1179)John Geebefore adoption28b649d
line 57why, one step back57 .name('prog')Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 58–68why, one step back58 .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 back69 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 back70 });71Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 72–76why, one step back72 // 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 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 77why, one step back77 .exitOverride((_err) => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 78–94why, one step back78 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 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 95why, one step back95 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 96–111why, one step back96 .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 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–123why, one step back113 .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 back124 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 back125 });126127 test('when specify --help then throw CommanderError', () => {128 const program = new commander.Command();129 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 130why, one step back130 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 131–145why, one step back131132 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 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 146why, one step back146 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 147–156why, one step back147 .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 back157 expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 158–163why, one step back158 });159160 test('when specify --version then throw CommanderError', () => {161 const myVersion = '1.2.3';162 const program = new commander.Command();163 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 164why, one step back164 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 165–177why, one step back165 .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 });177tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706
lines 178–179why, one step back178 test('when executableSubcommand succeeds then call exitOverride', async() => {179 expect.hasAssertions();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 180–181why, one step back180 const pm = path.join(__dirname, 'fixtures/pm');181 const program = new commander.Command();tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706
lines 182–190why, one step back182 await new Promise((resolve) => {183 program184 .exitOverride((err) => {185 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');186 resolve();187 })188 .command('silent', 'description');189 program.parse(['node', pm, 'silent']);190 });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
line 191why, one step back191 });Add .requiredOption() for mandatory options (#1071)John Geebefore adoption7f9edba
lines 192–208why, one step back192193 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 back209});