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

Commit 36ab100

Browse files
test(ngMock window.inject): test extending inject error stack info
Angular's ngMock inject() function, when called outside of a test spec context will not directly call the provided callback but will instead return a wrapper function to call the provided function at a later time, presumably while in some test spec context. And if that is the case, Angular would like to include the information on the inject() calling location to be included in the thrown error's stack trace information, so it manually appends it to the ones included in the actual error's stack trace. The added test makes sure this functionality: - works as expected in browsers supporting JavaScript stack trace collection, e.g. Chrome, Firefox, IE10+, Opera & PhantomJS - does not add any bogus stack track information in browsers that do not support JavaScript stack trace collection, e.g. IE8 or IE9 Closes #13591
1 parent a57286c commit 36ab100

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

test/ngMock/angular-mocksSpec.js

+47
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,53 @@ describe('ngMock', function() {
919919
});
920920
}).toThrow('test message');
921921
}));
922+
923+
describe('when called outside of test spec context and inject callback throws an Error', function() {
924+
// - IE9 does not support providing stack traces
925+
// - Chrome & Firefox give us the stack trace as soon as an Error is
926+
// created
927+
// - IE10, IE11 & PhantomJS give us the stack trace only once the error
928+
// is thrown
929+
var stackTraceSupported = (function () {
930+
var error = new Error();
931+
if (error.stack)
932+
return error.stack;
933+
try {
934+
throw error;
935+
} catch (e) {
936+
return e.stack;
937+
}
938+
})();
939+
940+
function testCaller() {
941+
return inject(function() {
942+
throw new Error();
943+
});
944+
}
945+
var throwErrorFromInjectCallback = testCaller();
946+
947+
if (stackTraceSupported) {
948+
describe('on browsers supporting stack traces', function() {
949+
it('should update thrown Error stack with inject call location', function() {
950+
try {
951+
throwErrorFromInjectCallback();
952+
} catch (e) {
953+
expect(e.stack).toMatch('testCaller');
954+
}
955+
});
956+
});
957+
} else {
958+
describe('on browsers not supporting stack traces', function() {
959+
it('should not add stack trace information to thrown Error', function() {
960+
try {
961+
throwErrorFromInjectCallback();
962+
} catch (e) {
963+
expect(e.stack).not.toBeDefined();
964+
}
965+
});
966+
});
967+
}
968+
});
922969
});
923970
});
924971

0 commit comments

Comments
 (0)