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

Commit

Permalink
fix(ngMock window.inject): Remove Error 'stack' property changes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wizardwerdna authored and vojtajina committed Jan 7, 2014
1 parent cdc4d48 commit 7e91645
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2079,6 +2079,20 @@ if(window.jasmine || window.mocha) {
*
* @param {...Function} fns any number of functions which will be injected using the injector.
*/



var ErrorAddingDeclarationLocationStack = function(e, errorForStack) {
this.message = e.message;
this.name = e.name;
if (e.line) this.line = e.line;
if (e.sourceId) this.sourceId = e.sourceId;
if (e.stack && errorForStack)
this.stack = e.stack + '\n' + errorForStack.stack;
if (e.stackArray) this.stackArray = e.stackArray;
};
ErrorAddingDeclarationLocationStack.prototype.toString = Error.prototype.toString;

window.inject = angular.mock.inject = function() {
var blockFns = Array.prototype.slice.call(arguments, 0);
var errorForStack = new Error('Declaration Location');
Expand All @@ -2099,7 +2113,9 @@ if(window.jasmine || window.mocha) {
injector.invoke(blockFns[i] || angular.noop, this);
/* jshint +W040 */
} catch (e) {
if(e.stack && errorForStack) e.stack += '\n' + errorForStack.stack;
if (e.stack && errorForStack) {
throw new ErrorAddingDeclarationLocationStack(e, errorForStack);
}
throw e;
} finally {
errorForStack = null;
Expand Down
17 changes: 17 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,23 @@ describe('ngMock', function() {
expect(log).toEqual('module;inject;')
});
});

// We don't run the following tests on IE8.
// IE8 throws "Object does not support this property or method." error,
// when thrown from a function defined on window (which `inject` is).
if (msie <= 8) return;

it('should not change thrown Errors', function() {
expect(function(){
throw new Error('test message');
}).toThrow('test message');
});

it('should not change thrown strings', function(){
expect(function(){
throw 'test message';
}).toThrow('test message');
});
});
});

Expand Down

0 comments on commit 7e91645

Please sign in to comment.