Skip to content

Commit 3a09c6d

Browse files
test(ngMock window.inject): error stack trace on inject with multiple functions
Injection function throwing an Error should update the thrown Error's stack trace information with the window.inject() call location information even when multiple injection functions are passed to the window.inject() call and non-initial provided function fails. Closes angular#13594.
1 parent 48f222c commit 3a09c6d

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

test/ngMock/angular-mocksSpec.js

+37-12
Original file line numberDiff line numberDiff line change
@@ -932,19 +932,28 @@ describe('ngMock', function() {
932932
// function returned by inject(), when called outside of test spec
933933
// context, may have stored state so do not reuse the result from this
934934
// call in multiple test specs
935-
function testInjectCaller() {
936-
var shouldThrow;
937-
// using an extra internalInjectCaller() wrapper here avoids stack trace
938-
// constructed by some browsers (e.g. FireFox) from containing the name
939-
// of the external caller function
940-
var injectingCall = (function internalInjectCaller() {
941-
return inject(function() {
942-
if (shouldThrow)
935+
function testInjectCaller(injectionFunctionCount) {
936+
var shouldThrow = [];
937+
// using an extra named function wrapper around the Error throw avoids
938+
// stack trace constructed by some browsers (e.g. FireFox) from
939+
// containing the name of the external caller function
940+
function injectionFunction(index) {
941+
return function() {
942+
if (shouldThrow[index])
943943
throw new Error();
944-
});
945-
})();
946-
injectingCall.setThrow = function(value) {
947-
shouldThrow = value;
944+
};
945+
}
946+
var injectionFunctions = [];
947+
for (var i = 0; i < (injectionFunctionCount || 1); ++i) {
948+
injectionFunctions.push(injectionFunction(i));
949+
}
950+
var injectingCall = inject.apply(window, injectionFunctions);
951+
injectingCall.setThrow = function(index, value) {
952+
if (!isDefined(value)) {
953+
value = index;
954+
index = 0;
955+
}
956+
shouldThrow[index] = value;
948957
};
949958
return injectingCall;
950959
}
@@ -1003,6 +1012,22 @@ describe('ngMock', function() {
10031012
}
10041013
});
10051014
});
1015+
1016+
describe('when called outside of test spec context with multiple injected functions', function() {
1017+
var injectingCall = testInjectCaller(2);
1018+
1019+
// regression test for issue #13594
1020+
// regression test for issue #13591 when run on IE10+ or PhantomJS
1021+
it('should update thrown Error stack when second injected function fails', function() {
1022+
injectingCall.setThrow(0, false);
1023+
injectingCall.setThrow(1, true);
1024+
try {
1025+
injectingCall();
1026+
} catch (e) {
1027+
expect(e.stack).toMatch('testInjectCaller');
1028+
}
1029+
});
1030+
});
10061031
});
10071032
}
10081033
});

0 commit comments

Comments
 (0)