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

Commit 7e91645

Browse files
wizardwerdnavojtajina
authored andcommitted
fix(ngMock window.inject): Remove Error 'stack' property changes
Recent browsers, particularly PhantomJS 1.9.2 and Safari 7.0 treat the stack property as non-configurable and unwritable. Because window.inject captures the stack at the time of the inject, and attempts to insert it into a captured throw from the injected function by modifying e.stack, a meaningless error message and stack is thrown instead. This commit inserts two tests exposing the problem, and implements a proposed solution that builds a new error-like object that mimicks the old Error object, but with the additional stack information, and captures the toString function from the Error object prototype. This appears to work for the browsers suppoerted here.
1 parent cdc4d48 commit 7e91645

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/ngMock/angular-mocks.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,20 @@ if(window.jasmine || window.mocha) {
20792079
*
20802080
* @param {...Function} fns any number of functions which will be injected using the injector.
20812081
*/
2082+
2083+
2084+
2085+
var ErrorAddingDeclarationLocationStack = function(e, errorForStack) {
2086+
this.message = e.message;
2087+
this.name = e.name;
2088+
if (e.line) this.line = e.line;
2089+
if (e.sourceId) this.sourceId = e.sourceId;
2090+
if (e.stack && errorForStack)
2091+
this.stack = e.stack + '\n' + errorForStack.stack;
2092+
if (e.stackArray) this.stackArray = e.stackArray;
2093+
};
2094+
ErrorAddingDeclarationLocationStack.prototype.toString = Error.prototype.toString;
2095+
20822096
window.inject = angular.mock.inject = function() {
20832097
var blockFns = Array.prototype.slice.call(arguments, 0);
20842098
var errorForStack = new Error('Declaration Location');
@@ -2099,7 +2113,9 @@ if(window.jasmine || window.mocha) {
20992113
injector.invoke(blockFns[i] || angular.noop, this);
21002114
/* jshint +W040 */
21012115
} catch (e) {
2102-
if(e.stack && errorForStack) e.stack += '\n' + errorForStack.stack;
2116+
if (e.stack && errorForStack) {
2117+
throw new ErrorAddingDeclarationLocationStack(e, errorForStack);
2118+
}
21032119
throw e;
21042120
} finally {
21052121
errorForStack = null;

test/ngMock/angular-mocksSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,23 @@ describe('ngMock', function() {
862862
expect(log).toEqual('module;inject;')
863863
});
864864
});
865+
866+
// We don't run the following tests on IE8.
867+
// IE8 throws "Object does not support this property or method." error,
868+
// when thrown from a function defined on window (which `inject` is).
869+
if (msie <= 8) return;
870+
871+
it('should not change thrown Errors', function() {
872+
expect(function(){
873+
throw new Error('test message');
874+
}).toThrow('test message');
875+
});
876+
877+
it('should not change thrown strings', function(){
878+
expect(function(){
879+
throw 'test message';
880+
}).toThrow('test message');
881+
});
865882
});
866883
});
867884

0 commit comments

Comments
 (0)