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

Commit a57286c

Browse files
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.
1 parent 2334a51 commit a57286c

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/ngMock/angular-mocks.js

+9
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,15 @@ if (window.jasmine || window.mocha) {
24012401
window.inject = angular.mock.inject = function() {
24022402
var blockFns = Array.prototype.slice.call(arguments, 0);
24032403
var errorForStack = new Error('Declaration Location');
2404+
// some browsers, e.g. PhanthomJS, do not set a new error object's stack
2405+
// information until it has been thrown
2406+
if (!errorForStack.stack) {
2407+
try {
2408+
throw errorForStack;
2409+
} catch (e) {
2410+
errorForStack = e;
2411+
}
2412+
}
24042413
return isSpecRunning() ? workFn.call(currentSpec) : workFn;
24052414
/////////////////////
24062415
function workFn() {

0 commit comments

Comments
 (0)