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

Commit 3d8060f

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 #13594.
1 parent 2969f55 commit 3d8060f

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
@@ -934,19 +934,28 @@ describe('ngMock', function() {
934934
// function returned by inject(), when called outside of test spec
935935
// context, may have stored state so do not reuse the result from this
936936
// call in multiple test specs
937-
function testInjectCaller() {
938-
var shouldThrow;
939-
// using an extra internalInjectCaller() wrapper here avoids stack trace
940-
// constructed by some browsers (e.g. FireFox) from containing the name
941-
// of the external caller function
942-
var injectingCall = (function internalInjectCaller() {
943-
return inject(function() {
944-
if (shouldThrow)
937+
function testInjectCaller(injectionFunctionCount) {
938+
var shouldThrow = [];
939+
// using an extra named function wrapper around the Error throw avoids
940+
// stack trace constructed by some browsers (e.g. FireFox) from
941+
// containing the name of the external caller function
942+
function injectionFunction(index) {
943+
return function() {
944+
if (shouldThrow[index])
945945
throw new Error();
946-
});
947-
})();
948-
injectingCall.setThrow = function(value) {
949-
shouldThrow = value;
946+
};
947+
}
948+
var injectionFunctions = [];
949+
for (var i = 0; i < (injectionFunctionCount || 1); ++i) {
950+
injectionFunctions.push(injectionFunction(i));
951+
}
952+
var injectingCall = inject.apply(window, injectionFunctions);
953+
injectingCall.setThrow = function(index, value) {
954+
if (!isDefined(value)) {
955+
value = index;
956+
index = 0;
957+
}
958+
shouldThrow[index] = value;
950959
};
951960
return injectingCall;
952961
}
@@ -1005,6 +1014,22 @@ describe('ngMock', function() {
10051014
}
10061015
});
10071016
});
1017+
1018+
describe('when called outside of test spec context with multiple injected functions', function() {
1019+
var injectingCall = testInjectCaller(2);
1020+
1021+
// regression test for issue #13594
1022+
// regression test for issue #13591 when run on IE10+ or PhantomJS
1023+
it('should update thrown Error stack when second injected function fails', function() {
1024+
injectingCall.setThrow(0, false);
1025+
injectingCall.setThrow(1, true);
1026+
try {
1027+
injectingCall();
1028+
} catch (e) {
1029+
expect(e.stack).toMatch('testInjectCaller');
1030+
}
1031+
});
1032+
});
10081033
});
10091034
}
10101035
});

0 commit comments

Comments
 (0)