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

Commit 51a27c0

Browse files
Shahar Talmipetebacondarwin
Shahar Talmi
authored andcommitted
feat(ngMock): invoke nested calls to module() immediately
Before 1.3, it was possible to call `angular.mock.module()` from inside another module. After this version additional calls were just ignored. It is helpful to be able to do this since a test may have helper functions that need to access "config"-time things such as constants or providers, and without this change it is difficult to get hold of the `$provide` object from within those helpers. Closes #12887
1 parent f02811f commit 51a27c0

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/ngMock/angular-mocks.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -2282,16 +2282,21 @@ if (window.jasmine || window.mocha) {
22822282
if (currentSpec.$injector) {
22832283
throw new Error('Injector already created, can not register a module!');
22842284
} else {
2285-
var modules = currentSpec.$modules || (currentSpec.$modules = []);
2285+
var fn, modules = currentSpec.$modules || (currentSpec.$modules = []);
22862286
angular.forEach(moduleFns, function(module) {
22872287
if (angular.isObject(module) && !angular.isArray(module)) {
2288-
modules.push(function($provide) {
2288+
fn = function($provide) {
22892289
angular.forEach(module, function(value, key) {
22902290
$provide.value(key, value);
22912291
});
2292-
});
2292+
};
22932293
} else {
2294-
modules.push(module);
2294+
fn = module;
2295+
}
2296+
if (currentSpec.$providerInjector) {
2297+
currentSpec.$providerInjector.invoke(fn);
2298+
} else {
2299+
modules.push(fn);
22952300
}
22962301
});
22972302
}
@@ -2405,6 +2410,9 @@ if (window.jasmine || window.mocha) {
24052410
function workFn() {
24062411
var modules = currentSpec.$modules || [];
24072412
var strictDi = !!currentSpec.$injectorStrict;
2413+
modules.unshift(function($injector) {
2414+
currentSpec.$providerInjector = $injector;
2415+
});
24082416
modules.unshift('ngMock');
24092417
modules.unshift('ng');
24102418
var injector = currentSpec.$injector;

test/ngMock/angular-mocksSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,19 @@ describe('ngMock', function() {
831831
});
832832
});
833833

834+
describe('nested calls', function() {
835+
it('should invoke nested module calls immediately', function() {
836+
module(function($provide) {
837+
$provide.constant('someConst', 'blah');
838+
module(function(someConst) {
839+
log = someConst;
840+
});
841+
});
842+
inject(function() {
843+
expect(log).toBe('blah');
844+
});
845+
});
846+
});
834847

835848
describe('inline in test', function() {
836849
it('should load module', function() {

0 commit comments

Comments
 (0)