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

fix(ngMock): Change ErrorAddingDeclarationLocationStack prototype #14344

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2774,7 +2774,7 @@ if (window.jasmine || window.mocha) {
this.stack = e.stack + '\n' + errorForStack.stack;
if (e.stackArray) this.stackArray = e.stackArray;
};
ErrorAddingDeclarationLocationStack.prototype.toString = Error.prototype.toString;
ErrorAddingDeclarationLocationStack.prototype = Error.prototype;

window.inject = angular.mock.inject = function() {
var blockFns = Array.prototype.slice.call(arguments, 0);
Expand Down
38 changes: 38 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,44 @@ describe('`afterEach` clean-up', function() {
});
});

describe('ErrorAddingDeclarationLocationStack', function() {
it('should be caught by jasmine\'s toThrowError', function() {
//create function that causes an error
function causeNgMocksError() {
module(function($provide) {
//cause $provide.factory to include an error
$provide.factory('causeError', function() {
//Say for example, some mock module caused an error when instantiated
throw new Error("Error!");
});
});

//call the injector on 'causeError' to make sure the provider is called
var causeError;
inject(function(_causeError_) {
causeError = _causeError_;
});
}

//The following is a simplified implementation of future Jasmine's .toThrowError()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by future Jasmine's .toThrowError() ?
Why can't you use it directly ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toThrowError() was introduced in Jasmine 2.0. It uses errorObject instanceof Error to check if the thrown object is an Error Object (for matching strings/regex against their .message property). Angular's build environment uses now >2-year-old Jasmine 1.3 for its own tests, so I had to rig a basic function for how Jasmine's toThrowError() fails when running this code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not any more :) We are on Jasmine 2 now 😎

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoo! Let me amend this PR in the coming days

// which is at the root of this problem
var error;
try {
causeNgMocksError();
} catch (e) {
error = e;
}

//Apologies for inelegant pass/fail conditions
if (error instanceof Error) {
//pass test
expect(true).toBe(true);
} else {
//fail test
throw new Error("Expected Error but got " + error);
}
});
});

describe('uninstantiated or falsy `$rootElement`', function() {
it('should not break if `$rootElement` was never instantiated', function() {
Expand Down