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

Commit 0c1b078

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 e3eb4bd commit 0c1b078

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

test/ngMock/angular-mocksSpec.js

+54-37
Original file line numberDiff line numberDiff line change
@@ -933,52 +933,69 @@ describe('ngMock', function() {
933933
}).toThrow('test message');
934934
}));
935935

936-
describe('when called outside of test spec context and inject callback throws an Error', function() {
937-
// - IE9 does not support providing stack traces
938-
// - Chrome & Firefox give us the stack trace as soon as an Error is
939-
// created
940-
// - IE10, IE11 & PhantomJS give us the stack trace only once the error
941-
// is thrown
942-
var stackTraceSupported = (function() {
943-
var error = new Error();
944-
if (error.stack)
945-
return error.stack;
946-
try {
947-
throw error;
948-
} catch (e) {
949-
return e.stack;
950-
}
951-
})();
936+
// - IE9 does not support providing stack traces
937+
// - Chrome & Firefox give us the stack trace as soon as an Error is
938+
// created
939+
// - IE10, IE11 & PhantomJS give us the stack trace only once the error is
940+
// thrown
941+
var stackTraceSupported = (function() {
942+
var error = new Error();
943+
if (error.stack)
944+
return error.stack;
945+
try {
946+
throw error;
947+
} catch (e) {
948+
return e.stack;
949+
}
950+
})();
952951

953-
function testCaller() {
952+
function testInjectCaller() {
953+
var shouldThrow;
954+
var injectingCall = (function internalInjectCaller() {
954955
return inject(function() {
955-
throw new Error();
956-
});
957-
}
958-
var throwErrorFromInjectCallback = testCaller();
959-
960-
if (stackTraceSupported) {
961-
describe('on browsers supporting stack traces', function() {
962-
it('should update thrown Error stack with inject call location', function() {
963-
try {
964-
throwErrorFromInjectCallback();
965-
} catch (e) {
966-
expect(e.stack).toMatch('testCaller');
967-
}
968-
});
956+
if (shouldThrow)
957+
throw new Error();
969958
});
970-
} else {
971-
describe('on browsers not supporting stack traces', function() {
972-
it('should not add stack trace information to thrown Error', function() {
959+
})();
960+
injectingCall.setThrow = function(value) {
961+
shouldThrow = value;
962+
};
963+
return injectingCall;
964+
}
965+
966+
if (!stackTraceSupported) {
967+
describe('on browsers not supporting stack traces', function() {
968+
describe('when called outside of test spec context', function() {
969+
var injectingCall = testInjectCaller();
970+
971+
it('should not add stack trace information to thrown injection Error', function() {
972+
injectingCall.setThrow(true);
973973
try {
974-
throwErrorFromInjectCallback();
974+
injectingCall();
975975
} catch (e) {
976976
expect(e.stack).not.toBeDefined();
977977
}
978978
});
979979
});
980-
}
981-
});
980+
});
981+
}
982+
983+
if (stackTraceSupported) {
984+
describe('on browsers supporting stack traces', function() {
985+
describe('when called outside of test spec context and injecting throws an Error', function() {
986+
var throwingInjectingCall = testInjectCaller();
987+
throwingInjectingCall.setThrow(true);
988+
989+
it('should update thrown Error stack with inject call location', function() {
990+
try {
991+
throwingInjectingCall();
992+
} catch (e) {
993+
expect(e.stack).toMatch('testInjectCaller');
994+
}
995+
});
996+
});
997+
});
998+
}
982999
});
9831000
});
9841001

0 commit comments

Comments
 (0)