Skip to content

Commit 42cecb9

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 0e52aeb commit 42cecb9

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

test/ngMock/angular-mocksSpec.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -915,52 +915,69 @@ describe('ngMock', function() {
915915
}).toThrow('test message');
916916
}));
917917

918-
describe('when called outside of test spec context and inject callback throws an Error', function() {
919-
// - IE9 does not support providing stack traces
920-
// - Chrome & Firefox give us the stack trace as soon as an Error is
921-
// created
922-
// - IE10, IE11 & PhantomJS give us the stack trace only once the error
923-
// is thrown
924-
var stackTraceSupported = (function() {
925-
var error = new Error();
926-
if (error.stack)
927-
return error.stack;
928-
try {
929-
throw error;
930-
} catch (e) {
931-
return e.stack;
932-
}
933-
})();
918+
// - IE9 does not support providing stack traces
919+
// - Chrome & Firefox give us the stack trace as soon as an Error is
920+
// created
921+
// - IE10, IE11 & PhantomJS give us the stack trace only once the error is
922+
// thrown
923+
var stackTraceSupported = (function() {
924+
var error = new Error();
925+
if (error.stack)
926+
return error.stack;
927+
try {
928+
throw error;
929+
} catch (e) {
930+
return e.stack;
931+
}
932+
})();
934933

935-
function testCaller() {
934+
function testInjectCaller() {
935+
var shouldThrow;
936+
var injectingCall = (function internalInjectCaller() {
936937
return inject(function() {
937-
throw new Error();
938-
});
939-
}
940-
var throwErrorFromInjectCallback = testCaller();
941-
942-
if (stackTraceSupported) {
943-
describe('on browsers supporting stack traces', function() {
944-
it('should update thrown Error stack with inject call location', function() {
945-
try {
946-
throwErrorFromInjectCallback();
947-
} catch (e) {
948-
expect(e.stack).toMatch('testCaller');
949-
}
950-
});
938+
if (shouldThrow)
939+
throw new Error();
951940
});
952-
} else {
953-
describe('on browsers not supporting stack traces', function() {
954-
it('should not add stack trace information to thrown Error', function() {
941+
})();
942+
injectingCall.setThrow = function(value) {
943+
shouldThrow = value;
944+
};
945+
return injectingCall;
946+
}
947+
948+
if (!stackTraceSupported) {
949+
describe('on browsers not supporting stack traces', function() {
950+
describe('when called outside of test spec context', function() {
951+
var injectingCall = testInjectCaller();
952+
953+
it('should not add stack trace information to thrown injection Error', function() {
954+
injectingCall.setThrow(true);
955955
try {
956-
throwErrorFromInjectCallback();
956+
injectingCall();
957957
} catch (e) {
958958
expect(e.stack).not.toBeDefined();
959959
}
960960
});
961961
});
962-
}
963-
});
962+
});
963+
}
964+
965+
if (stackTraceSupported) {
966+
describe('on browsers supporting stack traces', function() {
967+
describe('when called outside of test spec context and injecting throws an Error', function() {
968+
var throwingInjectingCall = testInjectCaller();
969+
throwingInjectingCall.setThrow(true);
970+
971+
it('should update thrown Error stack with inject call location', function() {
972+
try {
973+
throwingInjectingCall();
974+
} catch (e) {
975+
expect(e.stack).toMatch('testInjectCaller');
976+
}
977+
});
978+
});
979+
});
980+
}
964981
});
965982
});
966983

0 commit comments

Comments
 (0)