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

Commit bb752d1

Browse files
refactor(ngMock window.inject test): create room for planned tests
Split up `ngMock` `window.inject` tests into tests on browsers supporting fetching current stack trace information and those not supporting it, as we plan on adding new test specs to the first group. Required making the test inject caller function more flexible so it can be easily configured in different tests without unnecessary code duplication.
1 parent 56dae6f commit bb752d1

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

test/ngMock/angular-mocksSpec.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -920,50 +920,67 @@ describe('ngMock', function() {
920920
}).toThrow('test message');
921921
}));
922922

923-
describe('error stack trace when called outside of spec context', function() {
924-
// - Chrome, Firefox, Edge, Opera give us the stack trace as soon as an Error is created
925-
// - IE10+, PhantomJS give us the stack trace only once the error is thrown
926-
// - IE9 does not provide stack traces
927-
var stackTraceSupported = (function() {
928-
var error = new Error();
929-
if (!error.stack) {
930-
try {
931-
throw error;
932-
} catch (e) {}
933-
}
923+
// - Chrome, Firefox, Edge, Opera give us the stack trace as soon as an Error is created
924+
// - IE10+, PhantomJS give us the stack trace only once the error is thrown
925+
// - IE9 does not provide stack traces
926+
var stackTraceSupported = (function() {
927+
var error = new Error();
928+
if (!error.stack) {
929+
try {
930+
throw error;
931+
} catch (e) {}
932+
}
934933

935-
return !!error.stack;
936-
})();
934+
return !!error.stack;
935+
})();
937936

938-
function testCaller() {
937+
function testInjectCaller() {
938+
var shouldThrow;
939+
var injectingCall = (function internalInjectCaller() {
939940
return inject(function() {
940-
throw new Error();
941+
if (shouldThrow)
942+
throw new Error();
941943
});
942-
}
943-
var throwErrorFromInjectCallback = testCaller();
944+
})();
945+
injectingCall.setThrow = function(value) {
946+
shouldThrow = value;
947+
};
948+
return injectingCall;
949+
}
944950

945-
if (stackTraceSupported) {
946-
describe('on browsers supporting stack traces', function() {
947-
it('should update thrown Error stack trace with inject call location', function() {
951+
if (!stackTraceSupported) {
952+
describe('on browsers not supporting stack traces', function() {
953+
describe('when called outside of test spec context', function() {
954+
var injectingCall = testInjectCaller();
955+
956+
it('should not add stack trace information to thrown injection Error', function() {
957+
injectingCall.setThrow(true);
948958
try {
949-
throwErrorFromInjectCallback();
959+
injectingCall();
950960
} catch (e) {
951-
expect(e.stack).toMatch('testCaller');
961+
expect(e.stack).toBeUndefined();
952962
}
953963
});
954964
});
955-
} else {
956-
describe('on browsers not supporting stack traces', function() {
957-
it('should not add stack trace information to thrown Error', function() {
965+
});
966+
}
967+
968+
if (stackTraceSupported) {
969+
describe('on browsers supporting stack traces', function() {
970+
describe('when called outside of test spec context and initial inject callback invocation fails', function() {
971+
var throwingInjectingCall = testInjectCaller();
972+
throwingInjectingCall.setThrow(true);
973+
974+
it('should update thrown Error stack trace with inject call location', function() {
958975
try {
959-
throwErrorFromInjectCallback();
976+
throwingInjectingCall();
960977
} catch (e) {
961-
expect(e.stack).toBeUndefined();
978+
expect(e.stack).toMatch('testInjectCaller');
962979
}
963980
});
964981
});
965-
}
966-
});
982+
});
983+
}
967984
});
968985
});
969986

0 commit comments

Comments
 (0)