Skip to content

Commit 428f0b9

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 5d695e5 commit 428f0b9

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

test/ngMock/angular-mocksSpec.js

+46-29
Original file line numberDiff line numberDiff line change
@@ -915,50 +915,67 @@ describe('ngMock', function() {
915915
}).toThrow('test message');
916916
}));
917917

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

930-
return !!error.stack;
931-
})();
929+
return !!error.stack;
930+
})();
932931

933-
function testCaller() {
932+
function testInjectCaller() {
933+
var shouldThrow;
934+
var injectingCall = (function internalInjectCaller() {
934935
return inject(function() {
935-
throw new Error();
936+
if (shouldThrow)
937+
throw new Error();
936938
});
937-
}
938-
var throwErrorFromInjectCallback = testCaller();
939+
})();
940+
injectingCall.setThrow = function(value) {
941+
shouldThrow = value;
942+
};
943+
return injectingCall;
944+
}
939945

940-
if (stackTraceSupported) {
941-
describe('on browsers supporting stack traces', function() {
942-
it('should update thrown Error stack trace with inject call location', function() {
946+
if (!stackTraceSupported) {
947+
describe('on browsers not supporting stack traces', function() {
948+
describe('when called outside of test spec context', function() {
949+
var injectingCall = testInjectCaller();
950+
951+
it('should not add stack trace information to thrown injection Error', function() {
952+
injectingCall.setThrow(true);
943953
try {
944-
throwErrorFromInjectCallback();
954+
injectingCall();
945955
} catch (e) {
946-
expect(e.stack).toMatch('testCaller');
956+
expect(e.stack).toBeUndefined();
947957
}
948958
});
949959
});
950-
} else {
951-
describe('on browsers not supporting stack traces', function() {
952-
it('should not add stack trace information to thrown Error', function() {
960+
});
961+
}
962+
963+
if (stackTraceSupported) {
964+
describe('on browsers supporting stack traces', function() {
965+
describe('when called outside of test spec context and initial inject callback invocation fails', function() {
966+
var throwingInjectingCall = testInjectCaller();
967+
throwingInjectingCall.setThrow(true);
968+
969+
it('should update thrown Error stack trace with inject call location', function() {
953970
try {
954-
throwErrorFromInjectCallback();
971+
throwingInjectingCall();
955972
} catch (e) {
956-
expect(e.stack).toBeUndefined();
973+
expect(e.stack).toMatch('testInjectCaller');
957974
}
958975
});
959976
});
960-
}
961-
});
977+
});
978+
}
962979
});
963980
});
964981

0 commit comments

Comments
 (0)