You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
toThrowError is very useful, but I think it works incorrectly with custom error classes.
functionPirateError(message){this.message='Arrggghhhh! '+message;}PirateError.prototype=newError();describe('custom try/catch',function(){it('works fine in tests',function(){try{thrownewPirateError();}catch(error){expect(error).toEqual(jasmine.any(Error));expect(error).toEqual(jasmine.any(PirateError));expect(errorinstanceofError).toBeTruthy();expect(errorinstanceofPirateError).toBeTruthy();}});});describe('toThrowError',function(){it('considers error inheritance',function(){expect(function(){thrownewPirateError('Blow me down!');}).toThrowError(PirateError);});});
Result:
Expected function to throw PirateError, but it threw Error.
The text was updated successfully, but these errors were encountered:
Jasmine uses the constructor attribute to determine whether the thrown error is of the desired type. When you assign the prototype of your PirateError to be a new Error() it changes the constructor to the Error function instead. If you add:
PirateError.prototype.constructor=PirateError;
after you assign the prototype your example should work.
We'd be open to a way to do this checking that didn't require re-assigning the constructor after setting the prototype, but none has come up yet.
toThrowError
is very useful, but I think it works incorrectly with custom error classes.Result:
The text was updated successfully, but these errors were encountered: