From c72b88a44fe929ce7eefd342b468a989373bb3ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jurko=20Gospodneti=C4=87?= Date: Sat, 19 Dec 2015 17:31:49 +0100 Subject: [PATCH 1/2] fix(ngMock window.inject): fix collecting stack trace in PhantomJS Add support for collecting current stack trace information in browsers (e.g. PhantomJS) that do not automatically store the current stack trace information in a newly created `Error` object's `stack` property, but only add it there once the `Error` gets thrown. The original implementation works fine in Firefox & Chrome, but fails on PhantomJS where it, for example, breaks karma's error reporting in cases when an exception for thrown in a test like the following: ``` it('the holy crusade', inject(function () { var x = {}; x.holyGrail(); })); ``` where the ngMock `inject()` implementation would incorrectly add the word `undefined` to the end of the collected error stack trace information, thus causing the main error description to be reported back to karma as `undefined`. The whole `Error.stack` property and its behaviour is not standardized so there is no one true implementation that we can assume is used by all angular compatible browsers. MSDN JavaScript `stack Property (Error) (JavaScript)` docs found at http://msdn.microsoft.com/en-us/library/windows/apps/hh699850.aspx also seem to match the PhantomJS implementation. --- src/ngMock/angular-mocks.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 54347abe3a45..fa06f86877ee 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -2480,6 +2480,15 @@ if (window.jasmine || window.mocha) { window.inject = angular.mock.inject = function() { var blockFns = Array.prototype.slice.call(arguments, 0); var errorForStack = new Error('Declaration Location'); + // some browsers, e.g. PhanthomJS, do not set a new error object's stack + // information until it has been thrown + if (!errorForStack.stack) { + try { + throw errorForStack; + } catch (e) { + errorForStack = e; + } + } return isSpecRunning() ? workFn.call(currentSpec) : workFn; ///////////////////// function workFn() { From 6c45811b32068b7b28377fa90ad66bf0ce8bff8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jurko=20Gospodneti=C4=87?= Date: Sun, 20 Dec 2015 00:23:35 +0100 Subject: [PATCH 2/2] test(ngMock window.inject): test extending inject error stack info Angular's ngMock inject() function, when called outside of a test spec context will not directly call the provided callback but will instead return a wrapper function to call the provided function at a later time, presumably while in some test spec context. And if that is the case, Angular would like to include the information on the inject() calling location to be included in the thrown error's stack trace information, so it manually appends it to the ones included in the actual error's stack trace. The added test makes sure this functionality: - works as expected in browsers supporting JavaScript stack trace collection, e.g. Chrome, Firefox, IE10+, Opera & PhantomJS - does not add any bogus stack track information in browsers that do not support JavaScript stack trace collection, e.g. IE8 or IE9 Closes #13591 --- test/ngMock/angular-mocksSpec.js | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 8a0cdd62e8a5..c1142a8c991c 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -919,6 +919,53 @@ describe('ngMock', function() { }); }).toThrow('test message'); })); + + describe('when called outside of test spec context and inject callback throws an Error', function() { + // - IE9 does not support providing stack traces + // - Chrome & Firefox give us the stack trace as soon as an Error is + // created + // - IE10, IE11 & PhantomJS give us the stack trace only once the error + // is thrown + var stackTraceSupported = (function() { + var error = new Error(); + if (error.stack) + return error.stack; + try { + throw error; + } catch (e) { + return e.stack; + } + })(); + + function testCaller() { + return inject(function() { + throw new Error(); + }); + } + var throwErrorFromInjectCallback = testCaller(); + + if (stackTraceSupported) { + describe('on browsers supporting stack traces', function() { + it('should update thrown Error stack with inject call location', function() { + try { + throwErrorFromInjectCallback(); + } catch (e) { + expect(e.stack).toMatch('testCaller'); + } + }); + }); + } else { + describe('on browsers not supporting stack traces', function() { + it('should not add stack trace information to thrown Error', function() { + try { + throwErrorFromInjectCallback(); + } catch (e) { + expect(e.stack).not.toBeDefined(); + } + }); + }); + } + }); }); });