command.executableSubcommand.mock.test.js
This file
Path
tests/command.executableSubcommand.mock.test.js
79 lines · 3.5 KiB · JavaScript
Last changed by
Add test for process.exit finishing bunch of if-then-else cases (#1487)John Geebefore adoption0fa4b8d
History of this fileTake it
Add mock tests to cover subcommand error handling (#1474)John Geebefore adoption948796d
lines 1–43why, one step back1const commander = require('../');2const childProcess = require('child_process');3const EventEmitter = require('events');45// Using mock to allow try/catch around what is otherwise out-of-stack error handling.6// Injecting errors, these are not end-to-end tests.78function makeSystemError(code) {9 // We can not make an actual SystemError, but our usage is lightweight so easy to match.10 const err = new Error();11 err.code = code;12 return err;13}1415test('when subcommand executable missing (ENOENT) then throw custom message', () => {16 // If the command is not found, we show a custom error with an explanation and offer17 // some advice for possible fixes.18 const mockProcess = new EventEmitter();19 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { return mockProcess; });20 const program = new commander.Command();21 program.exitOverride();22 program.command('executable', 'executable description');23 program.parse(['executable'], { from: 'user' });24 expect(() => {25 mockProcess.emit('error', makeSystemError('ENOENT'));26 }).toThrow('use the executableFile option to supply a custom name'); // part of custom message27 spawnSpy.mockRestore();28});2930test('when subcommand executable not executable (EACCES) then throw custom message', () => {31 // Side note: this error does not actually happen on Windows! But we can still simulate the behaviour on other platforms.32 const mockProcess = new EventEmitter();33 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { return mockProcess; });34 const program = new commander.Command();35 program.exitOverride();36 program.command('executable', 'executable description');37 program.parse(['executable'], { from: 'user' });38 expect(() => {39 mockProcess.emit('error', makeSystemError('EACCES'));40 }).toThrow('not executable'); // part of custom message41 spawnSpy.mockRestore();42});43Add test for process.exit finishing bunch of if-then-else cases (#1487)John Geebefore adoption0fa4b8d
line 44why, one step back44test('when subcommand executable fails with other error and exitOverride then return in custom wrapper', () => {Add mock tests to cover subcommand error handling (#1474)John Geebefore adoption948796d
lines 45–62why, one step back45 // The existing behaviour is to just silently fail for unexpected errors, as it is happening46 // asynchronously in spawned process and client can not catch errors.47 const mockProcess = new EventEmitter();48 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { return mockProcess; });49 const program = new commander.Command();50 program.exitOverride((err) => {51 throw err;52 });53 program.command('executable', 'executable description');54 program.parse(['executable'], { from: 'user' });55 let caughtErr;56 try {57 mockProcess.emit('error', makeSystemError('OTHER'));58 } catch (err) {59 caughtErr = err;60 }61 expect(caughtErr.code).toEqual('commander.executeSubCommandAsync');62 expect(caughtErr.nestedError.code).toEqual('OTHER');Add test for process.exit finishing bunch of if-then-else cases (#1487)John Geebefore adoption0fa4b8d
lines 63–77why, one step back63 spawnSpy.mockRestore();64});6566test('when subcommand executable fails with other error then exit', () => {67 // The existing behaviour is to just silently fail for unexpected errors, as it is happening68 // asynchronously in spawned process and client can not catch errors.69 const mockProcess = new EventEmitter();70 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { return mockProcess; });71 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });72 const program = new commander.Command();73 program.command('executable', 'executable description');74 program.parse(['executable'], { from: 'user' });75 mockProcess.emit('error', makeSystemError('OTHER'));76 expect(exitSpy).toHaveBeenCalledWith(1);77 exitSpy.mockRestore();Add mock tests to cover subcommand error handling (#1474)John Geebefore adoption948796d
lines 78–79why, one step back78 spawnSpy.mockRestore();79});