.toThrow() does not work

Cing Sian Dal
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

--

--

Cing Sian Dal
Cing Sian Dal

Written by Cing Sian Dal

Don’t follow me. I wrote junks here. Follow me on Twitter instead.

No responses yet