ngMock: ErrorAddingDeclarationLocationStack is no longer matched by toThrowError since jasmine 2.4.1 #13821
Description
Environment:
- ngMock 1.4.3 (I know it's old, but I can confirm the issue is not about ngMock version not being the latest)
- karma-jasmine 0.3.6
- jasmine-core 2.4.1
I try to match error thrown in my code:
angular.module('foo').constant('bar', () => {
throw new Error('My error msg');
});
with the following test:
it('should throw expected message', () => {
module('foo');
inject(function (bar) {
expect(() => {
bar();
}).toThrowError('My error msg');
});
});
but I get error in the console:
Expected function to throw 'My error msg', but it threw Error: My error msg.
this is error from [email protected] toThrowError matcher.
Jasmine stopped comparing by constructor
in favour of using instanceof
operator in April 2015
The problem is caused by 7e916455b3
I tried modifying:
ErrorAddingDeclarationLocationStack.prototype.toString = Error.prototype.toString;
which was ok when constructor
was checked in a way that ErrorAddingDeclarationLocationStack
has Error
in its prototype chain:
ErrorAddingDeclarationLocationStack.prototype = Object.create(Error.prototype);
ErrorAddingDeclarationLocationStack.prototype.constructor = ErrorAddingDeclarationLocationStack;
and I can confirm it fixes the issue (error is considered an actual error again by [email protected])
@wizardwerdna can you please review this change? I'm not sure if you replaced only toString
on a prototype for a reason and if it's safe to do what I suggest. I think it may not be safe to do so and this change can reverse what you achieved. In such case a better way has to be found to circumnavigate the issue you fixed in order to be able to keep using [email protected] with ngMock.