Julien Moreau Julien Moreau - 1 year ago 125
TypeScript Question

Can't seem to catch error when using Jasmine toThrowError

I beginning to try jasmine and I want to use the toThrowError() function but my test does not want to succeed.

I have one function where I throw an error:


if(list == null){
throw new TypeError();
// do something...

And my test :

it('shall throw an error', inject()
testService: TestService
) => {
let test = testService.test(null);

And my test fails with an Uncaught TypeError (when I call it I'm doing it in a try catch).

Answer Source

You should expect a function, that calls your function with null, to throw an error. Right now you're expecting the result of calling the function to throw an error, which isn't happening.

expect(() => testService.test(null)).toThrowError(TypeError);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download