diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index fb7ef0573c20..b53b4e0264d8 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -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'); @@ -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; diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index ef7e88fae6f8..6c096471d2be 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -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'); + }); }); });