Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ngMock: ErrorAddingDeclarationLocationStack is no longer matched by toThrowError since jasmine 2.4.1 #13821

Closed
@jrencz

Description

@jrencz

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions