EN
JavaScript - catch exception in Jest test
6 points
In this short article, we would like to show how to catch expected exception writing tests in Jest in JavaScript.
Quick solution:
xxxxxxxxxx
1
test('test name here ...', () => {
2
expect(() => doSomeLogic()).toThrow();
3
});
xxxxxxxxxx
1
import doSomeLogic from './doSomeLogic';
2
3
describe('Some logic', () => {
4
test('throws always exception', () => {
5
expect(() => doSomeLogic()).toThrow();
6
});
7
});
Output:
doSomeLogic.js
file:
xxxxxxxxxx
1
const doSomeLogic = () => {
2
throw new Error('Some exception message here ...');
3
};
4
5
export default doSomeLogic;