Skip to content

Commit 0ce25ca

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 0816eca commit 0ce25ca

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
@@ -939,19 +939,28 @@ describe('ngMock', function() {
939939
// function returned by inject(), when called outside of test spec
940940
// context, may have stored state so do not reuse the result from this
941941
// call in multiple test specs
942-
function testInjectCaller() {
943-
var shouldThrow;
944-
// using an extra internalInjectCaller() wrapper here avoids stack trace
945-
// constructed by some browsers (e.g. FireFox) from containing the name
946-
// of the external caller function
947-
var injectingCall = (function internalInjectCaller() {
948-
return inject(function() {
949-
if (shouldThrow)
942+
function testInjectCaller(injectionFunctionCount) {
943+
var shouldThrow = [];
944+
// using an extra named function wrapper around the Error throw avoids
945+
// stack trace constructed by some browsers (e.g. FireFox) from
946+
// containing the name of the external caller function
947+
function injectionFunction(index) {
948+
return function() {
949+
if (shouldThrow[index])
950950
throw new Error();
951-
});
952-
})();
953-
injectingCall.setThrow = function(value) {
954-
shouldThrow = value;
951+
};
952+
}
953+
var injectionFunctions = [];
954+
for (var i = 0; i < (injectionFunctionCount || 1); ++i) {
955+
injectionFunctions.push(injectionFunction(i));
956+
}
957+
var injectingCall = inject.apply(window, injectionFunctions);
958+
injectingCall.setThrow = function(index, value) {
959+
if (!isDefined(value)) {
960+
value = index;
961+
index = 0;
962+
}
963+
shouldThrow[index] = value;
955964
};
956965
return injectingCall;
957966
}
@@ -1010,6 +1019,22 @@ describe('ngMock', function() {
10101019
}
10111020
});
10121021
});
1022+
1023+
describe('when called outside of test spec context with multiple injected functions', function() {
1024+
var injectingCall = testInjectCaller(2);
1025+
1026+
// regression test for issue #13594
1027+
// regression test for issue #13591 when run on IE10+ or PhantomJS
1028+
it('should update thrown Error stack when second injected function fails', function() {
1029+
injectingCall.setThrow(0, false);
1030+
injectingCall.setThrow(1, true);
1031+
try {
1032+
injectingCall();
1033+
} catch (e) {
1034+
expect(e.stack).toMatch('testInjectCaller');
1035+
}
1036+
});
1037+
});
10131038
});
10141039
}
10151040
});

0 commit comments

Comments
 (0)