1

Suppose I have a function like this:

const f = async () => {
  throw new Error('Huh???!');
};

And I want to test that it throws a RangeError (not just an Error) using Jest.

test('f throws a RangeError', () => {
  expect(f()).rejects.toThrowError(RangeError);
});

However this test passes.

How can I check the type of the error thrown asyncronously in Jest?

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245

1 Answers1

2

You can use the toBeInstanceOf matcher after rejects:

test('f throws a RangeError', () => {
  expect(f()).rejects.toBeInstanceOf(RangeError);
});
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245