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

Commit 2009aa0

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 456e15a commit 2009aa0

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
@@ -952,19 +952,28 @@ describe('ngMock', function() {
952952
// function returned by inject(), when called outside of test spec
953953
// context, may have stored state so do not reuse the result from this
954954
// call in multiple test specs
955-
function testInjectCaller() {
956-
var shouldThrow;
957-
// using an extra internalInjectCaller() wrapper here avoids stack trace
958-
// constructed by some browsers (e.g. FireFox) from containing the name
959-
// of the external caller function
960-
var injectingCall = (function internalInjectCaller() {
961-
return inject(function() {
962-
if (shouldThrow)
955+
function testInjectCaller(injectionFunctionCount) {
956+
var shouldThrow = [];
957+
// using an extra named function wrapper around the Error throw avoids
958+
// stack trace constructed by some browsers (e.g. FireFox) from
959+
// containing the name of the external caller function
960+
function injectionFunction(index) {
961+
return function () {
962+
if (shouldThrow[index])
963963
throw new Error();
964-
});
965-
})();
966-
injectingCall.setThrow = function(value) {
967-
shouldThrow = value;
964+
};
965+
}
966+
var injectionFunctions = [];
967+
for (var i = 0; i < (injectionFunctionCount || 1); ++i) {
968+
injectionFunctions.push(injectionFunction(i));
969+
}
970+
var injectingCall = inject.apply(window, injectionFunctions);
971+
injectingCall.setThrow = function(index, value) {
972+
if (!isDefined(value)) {
973+
value = index;
974+
index = 0;
975+
}
976+
shouldThrow[index] = value;
968977
};
969978
return injectingCall;
970979
}
@@ -1023,6 +1032,22 @@ describe('ngMock', function() {
10231032
}
10241033
});
10251034
});
1035+
1036+
describe('when called outside of test spec context with multiple injected functions', function() {
1037+
var injectingCall = testInjectCaller(2);
1038+
1039+
// regression test for issue #13594
1040+
// regression test for issue #13591 when run on IE10+ or PhantomJS
1041+
it('should update thrown Error stack when second injected function fails', function () {
1042+
injectingCall.setThrow(0, false);
1043+
injectingCall.setThrow(1, true);
1044+
try {
1045+
injectingCall();
1046+
} catch (e) {
1047+
expect(e.stack).toMatch('testInjectCaller');
1048+
}
1049+
});
1050+
});
10261051
});
10271052
}
10281053
});

0 commit comments

Comments
 (0)