Skip to content

Instantly share code, notes, and snippets.

@brandonweis
Last active June 17, 2020 12:20
Show Gist options
  • Select an option

  • Save brandonweis/f195ba645d46b957171cc868dbdf0122 to your computer and use it in GitHub Desktop.

Select an option

Save brandonweis/f195ba645d46b957171cc868dbdf0122 to your computer and use it in GitHub Desktop.
const caughtFunction = () => {
try {
blarg;
} catch (e) {
throw new Error('Ghaaa!');
}
}
const throwingFunction = () => {
throw new Error('Ghaaa!');
}
const badFunction = () => {
blarg;
}
describe('toThrow', function() {
beforeEach(() => {
jest.spyOn(console, 'error')
console.error.mockImplementation(() => {})
})
afterEach(() => {
console.error.mockRestore()
})
// When asserting .toThrow, the error is expected.
// So this should not pollute the terminal output with a stacktrace:
it('should not vomit to console', () => {
// This is nicely silent:
expect(() => {
caughtFunction();
}).toThrow('Ghaaa!')
// and this too:
expect(() => {
throwingFunction();
}).toThrow('Ghaaa!')
// and even this...:
expect(() => {
badFunction();
}).toThrow()
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment