Skip to content
Myr

command.executableSubcommand.mock.test.js

This file

Path

tests/command.executableSubcommand.mock.test.js

87 lines · 3.5 KiB · JavaScript

Last changed by

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

History of this file

Take it

The walk stopped after 200 commits: lines older than 950f742 point at it.

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

lines 1–18why, one step back
1const 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();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 19–21why, one step back
19 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => {
20 return mockProcess;21 });

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

lines 22–34why, one step back
22 const program = new commander.Command();
23 program.exitOverride();24 program.command('executable', 'executable description');25 program.parse(['executable'], { from: 'user' });26 expect(() => {27 mockProcess.emit('error', makeSystemError('ENOENT'));28 }).toThrow('use the executableFile option to supply a custom name'); // part of custom message29 spawnSpy.mockRestore();30});3132test('when subcommand executable not executable (EACCES) then throw custom message', () => {33 // Side note: this error does not actually happen on Windows! But we can still simulate the behaviour on other platforms.34 const mockProcess = new EventEmitter();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 35–37why, one step back
35 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => {
36 return mockProcess;37 });

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

lines 38–51why, one step back
38 const program = new commander.Command();
39 program.exitOverride();40 program.command('executable', 'executable description');41 program.parse(['executable'], { from: 'user' });42 expect(() => {43 mockProcess.emit('error', makeSystemError('EACCES'));44 }).toThrow('not executable'); // part of custom message45 spawnSpy.mockRestore();46});4748test('when subcommand executable fails with other error and exitOverride then return in custom wrapper', () => {49 // The existing behaviour is to just silently fail for unexpected errors, as it is happening50 // asynchronously in spawned process and client can not catch errors.51 const mockProcess = new EventEmitter();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 52–54why, one step back
52 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => {
53 return mockProcess;54 });

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

lines 55–75why, one step back
55 const program = new commander.Command();
56 program.exitOverride((err) => {57 throw err;58 });59 program.command('executable', 'executable description');60 program.parse(['executable'], { from: 'user' });61 let caughtErr;62 try {63 mockProcess.emit('error', makeSystemError('OTHER'));64 } catch (err) {65 caughtErr = err;66 }67 expect(caughtErr.code).toEqual('commander.executeSubCommandAsync');68 expect(caughtErr.nestedError.code).toEqual('OTHER');69 spawnSpy.mockRestore();70});7172test('when subcommand executable fails with other error then exit', () => {73 // The existing behaviour is to just silently fail for unexpected errors, as it is happening74 // asynchronously in spawned process and client can not catch errors.75 const mockProcess = new EventEmitter();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 76–79why, one step back
76 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => {
77 return mockProcess;78 });79 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

lines 80–87why, one step back
80 const program = new commander.Command();
81 program.command('executable', 'executable description');82 program.parse(['executable'], { from: 'user' });83 mockProcess.emit('error', makeSystemError('OTHER'));84 expect(exitSpy).toHaveBeenCalledWith(1);85 exitSpy.mockRestore();86 spawnSpy.mockRestore();87});