Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error() for displaying errors from client code (#1675)
* Refactor private _displayError() into public error() * add TypeScript * Add tests * Add to README * Tiny wording change
- Loading branch information
1 parent
772eb53
commit 7a59df4
Showing
6 changed files
with
123 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const commander = require('../'); | ||
|
||
test('when error called with message then message displayed on stderr', () => { | ||
const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { }); | ||
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { }); | ||
|
||
const program = new commander.Command(); | ||
const message = 'Goodbye'; | ||
program.error(message); | ||
|
||
expect(stderrSpy).toHaveBeenCalledWith(`${message}\n`); | ||
stderrSpy.mockRestore(); | ||
exitSpy.mockRestore(); | ||
}); | ||
|
||
test('when error called with no exitCode then process.exit(1)', () => { | ||
const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { }); | ||
|
||
const program = new commander.Command(); | ||
program.configureOutput({ | ||
writeErr: () => {} | ||
}); | ||
|
||
program.error('Goodbye'); | ||
|
||
expect(exitSpy).toHaveBeenCalledWith(1); | ||
exitSpy.mockRestore(); | ||
}); | ||
|
||
test('when error called with exitCode 2 then process.exit(2)', () => { | ||
const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { }); | ||
|
||
const program = new commander.Command(); | ||
program.configureOutput({ | ||
writeErr: () => {} | ||
}); | ||
program.error('Goodbye', { exitCode: 2 }); | ||
|
||
expect(exitSpy).toHaveBeenCalledWith(2); | ||
exitSpy.mockRestore(); | ||
}); | ||
|
||
test('when error called with code and exitOverride then throws with code', () => { | ||
const program = new commander.Command(); | ||
let errorThrown; | ||
program | ||
.exitOverride((err) => { errorThrown = err; throw err; }) | ||
.configureOutput({ | ||
writeErr: () => {} | ||
}); | ||
|
||
const code = 'commander.test'; | ||
expect(() => { | ||
program.error('Goodbye', { code }); | ||
}).toThrow(); | ||
expect(errorThrown.code).toEqual(code); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters