command.error.test.js
This file
Path
tests/command.error.test.js
62 lines · 1.6 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 1–3why, one step back1const commander = require('../');23test('when error called with message then message displayed on stderr', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 4–7why, one step back4 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});5 const stderrSpy = jest6 .spyOn(process.stderr, 'write')7 .mockImplementation(() => {});Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 8–18why, one step back89 const program = new commander.Command();10 const message = 'Goodbye';11 program.error(message);1213 expect(stderrSpy).toHaveBeenCalledWith(`${message}\n`);14 stderrSpy.mockRestore();15 exitSpy.mockRestore();16});1718test('when error called with no exitCode then process.exit(1)', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 19why, one step back19 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 20–22why, one step back2021 const program = new commander.Command();22 program.configureOutput({Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 23why, one step back23 writeErr: () => {},Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 24–32why, one step back24 });2526 program.error('Goodbye');2728 expect(exitSpy).toHaveBeenCalledWith(1);29 exitSpy.mockRestore();30});3132test('when error called with exitCode 2 then process.exit(2)', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 33why, one step back33 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 34–36why, one step back3435 const program = new commander.Command();36 program.configureOutput({Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 37why, one step back37 writeErr: () => {},Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 38–48why, one step back38 });39 program.error('Goodbye', { exitCode: 2 });4041 expect(exitSpy).toHaveBeenCalledWith(2);42 exitSpy.mockRestore();43});4445test('when error called with code and exitOverride then throws with code', () => {46 const program = new commander.Command();47 let errorThrown;48 programReformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 49–52why, one step back49 .exitOverride((err) => {50 errorThrown = err;51 throw err;52 })Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 53why, one step back53 .configureOutput({Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 54why, one step back54 writeErr: () => {},Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 55–62why, one step back55 });5657 const code = 'commander.test';58 expect(() => {59 program.error('Goodbye', { code });60 }).toThrow();61 expect(errorThrown.code).toEqual(code);62});