.toThrow() does not work
May 4, 2021
Normally you expect some functions to throw errors, you write this:
async function foo(){
throw new Error('some error');
}test('catch throws', async ()=>{
const result = await foo();
expect(result).toThrow();
});
Well, it does not work.
But this works:
async function foo(){
throw new Error('some error');
}test('catch throw', async ()=>{
const result = ()=> await foo();
expect(result).rejects.toThrow();
});
- Expectation needs a function argument.
- Prefix method is followed after
toThrow()
.
Read more at https://jestjs.io/docs/en/asynchronous#resolves-rejects