[RECIPE]: Check error throw in jest
![[RECIPE]: Check error throw in jest](https://res-1.cloudinary.com/hjl1petaf/image/upload/q_auto/v1/ghost-blog-images/20190525_145723.jpg)
Jest is something which millions of people uses everyday, a lot of questions are answered. But I keep finding something new for me every day.
This is not gonna work:
describe('throw test', () => {
it('should throw the error', () => {
expect(throwingFunc()).toThrow();
});
});
Test will fail with the error you are looking for.
But this will do the trick:
describe('throw test', () => {
it('should throw the error', () => {
expect(() => throwingFunc()).toThrow();
});
});
The only difference is the usage of arrow function.
Write readable, simple and meaningful tests!