command.error.test.js
This file
Path
tests/command.error.test.js
57 lines · 1.6 KiB · JavaScript
Last changed by
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
History of this fileTake it
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 1–57why, one step back1const commander = require('../');23test('when error called with message then message displayed on stderr', () => {4 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });5 const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });67 const program = new commander.Command();8 const message = 'Goodbye';9 program.error(message);1011 expect(stderrSpy).toHaveBeenCalledWith(`${message}\n`);12 stderrSpy.mockRestore();13 exitSpy.mockRestore();14});1516test('when error called with no exitCode then process.exit(1)', () => {17 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });1819 const program = new commander.Command();20 program.configureOutput({21 writeErr: () => {}22 });2324 program.error('Goodbye');2526 expect(exitSpy).toHaveBeenCalledWith(1);27 exitSpy.mockRestore();28});2930test('when error called with exitCode 2 then process.exit(2)', () => {31 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });3233 const program = new commander.Command();34 program.configureOutput({35 writeErr: () => {}36 });37 program.error('Goodbye', { exitCode: 2 });3839 expect(exitSpy).toHaveBeenCalledWith(2);40 exitSpy.mockRestore();41});4243test('when error called with code and exitOverride then throws with code', () => {44 const program = new commander.Command();45 let errorThrown;46 program47 .exitOverride((err) => { errorThrown = err; throw err; })48 .configureOutput({49 writeErr: () => {}50 });5152 const code = 'commander.test';53 expect(() => {54 program.error('Goodbye', { code });55 }).toThrow();56 expect(errorThrown.code).toEqual(code);57});