Skip to main content
Active reading [<https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s> <https://www.youtube.com/watch?v=1Dax90QyXgI&t=19m05s>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

How to test the type of a thrown exception in Jest

I'm working with some code where I need to test the type of an exception thrown by a function (Isis it TypeError, ReferenceError, etc.?).

My current testing framework is AVA and I can test it as a second argument t.throws method, like here:

it('should throw Error with message \'UNKNOWN ERROR\' when no params were passed', (t) => { const error = t.throws(() => { throwError(); }, TypeError); t.is(error.message, 'UNKNOWN ERROR'); }); 

I started rewriting my tests toin Jest and couldn't find how to easily do that. Is it even possible?

How to test type of thrown exception in Jest

I'm working with some code where I need to test type of exception thrown by function (Is it TypeError, ReferenceError etc.).

My current testing framework is AVA and I can test it as a second argument t.throws method, like here:

it('should throw Error with message \'UNKNOWN ERROR\' when no params were passed', (t) => { const error = t.throws(() => { throwError(); }, TypeError); t.is(error.message, 'UNKNOWN ERROR'); }); 

I started rewriting my tests to Jest and couldn't find how to easily do that. Is it even possible?

How to test the type of a thrown exception in Jest

I'm working with some code where I need to test the type of an exception thrown by a function (is it TypeError, ReferenceError, etc.?).

My current testing framework is AVA and I can test it as a second argument t.throws method, like here:

it('should throw Error with message \'UNKNOWN ERROR\' when no params were passed', (t) => { const error = t.throws(() => { throwError(); }, TypeError); t.is(error.message, 'UNKNOWN ERROR'); }); 

I started rewriting my tests in Jest and couldn't find how to easily do that. Is it even possible?

Source Link
bartsmykla
  • 6.1k
  • 2
  • 12
  • 6

How to test type of thrown exception in Jest

I'm working with some code where I need to test type of exception thrown by function (Is it TypeError, ReferenceError etc.).

My current testing framework is AVA and I can test it as a second argument t.throws method, like here:

it('should throw Error with message \'UNKNOWN ERROR\' when no params were passed', (t) => { const error = t.throws(() => { throwError(); }, TypeError); t.is(error.message, 'UNKNOWN ERROR'); }); 

I started rewriting my tests to Jest and couldn't find how to easily do that. Is it even possible?