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

Commit 0baa17a

Browse files
shahatapetebacondarwin
authored andcommitted
feat(ngMocks): cleanup $inject annotations after each test
this will help in detecting unannotated functions both in apps and angular core in case only part of the tests are run with strictDi
1 parent 2ece1c9 commit 0baa17a

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

src/auto/injector.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ function createInjector(modulesToLoad, strictDi) {
800800
}
801801

802802
var args = [],
803-
$inject = annotate(fn, strictDi, serviceName),
803+
$inject = createInjector.$$annotate(fn, strictDi, serviceName),
804804
length, i,
805805
key;
806806

@@ -839,7 +839,7 @@ function createInjector(modulesToLoad, strictDi) {
839839
invoke: invoke,
840840
instantiate: instantiate,
841841
get: getService,
842-
annotate: annotate,
842+
annotate: createInjector.$$annotate,
843843
has: function(name) {
844844
return providerCache.hasOwnProperty(name + providerSuffix) || cache.hasOwnProperty(name);
845845
}

src/ngMock/angular-mocks.js

+14
Original file line numberDiff line numberDiff line change
@@ -2127,18 +2127,32 @@ angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) {
21272127
if (window.jasmine || window.mocha) {
21282128

21292129
var currentSpec = null,
2130+
annotatedFunctions,
21302131
isSpecRunning = function() {
21312132
return !!currentSpec;
21322133
};
21332134

2135+
angular.mock.$$annotate = angular.injector.$$annotate;
2136+
angular.injector.$$annotate = function(fn) {
2137+
if (typeof fn === 'function' && !fn.$inject) {
2138+
annotatedFunctions.push(fn);
2139+
}
2140+
return angular.mock.$$annotate.apply(this, arguments);
2141+
};
2142+
21342143

21352144
(window.beforeEach || window.setup)(function() {
2145+
annotatedFunctions = [];
21362146
currentSpec = this;
21372147
});
21382148

21392149
(window.afterEach || window.teardown)(function() {
21402150
var injector = currentSpec.$injector;
21412151

2152+
annotatedFunctions.forEach(function(fn) {
2153+
delete fn.$inject;
2154+
});
2155+
21422156
angular.forEach(currentSpec.$modules, function(module) {
21432157
if (module && module.$$hashKey) {
21442158
module.$$hashKey = undefined;

test/auto/injectorSpec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ describe('injector', function() {
238238

239239

240240
it('should publish annotate API', function() {
241-
expect(injector.annotate).toBe(annotate);
241+
expect(angular.mock.$$annotate).toBe(annotate);
242+
spyOn(angular.mock, '$$annotate').andCallThrough();
243+
function fn() {}
244+
injector.annotate(fn);
245+
expect(angular.mock.$$annotate).toHaveBeenCalledWith(fn);
242246
});
243247
});
244248

test/ngMock/angular-mocksSpec.js

+33
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,39 @@ describe('ngMock', function() {
787787
expect(testFn.$$hashKey).toBeUndefined();
788788
});
789789
});
790+
791+
describe('$inject cleanup', function() {
792+
function testFn() {
793+
794+
}
795+
796+
it('should add $inject when invoking test function', inject(function($injector) {
797+
$injector.invoke(testFn);
798+
expect(testFn.$inject).toBeDefined();
799+
}));
800+
801+
it('should cleanup $inject after previous test', function() {
802+
expect(testFn.$inject).toBeUndefined();
803+
});
804+
805+
it('should add $inject when annotating test function', inject(function($injector) {
806+
$injector.annotate(testFn);
807+
expect(testFn.$inject).toBeDefined();
808+
}));
809+
810+
it('should cleanup $inject after previous test', function() {
811+
expect(testFn.$inject).toBeUndefined();
812+
});
813+
814+
it('should invoke an already annotated function', inject(function($injector) {
815+
testFn.$inject = [];
816+
$injector.invoke(testFn);
817+
}));
818+
819+
it('should not cleanup $inject after previous test', function() {
820+
expect(testFn.$inject).toBeDefined();
821+
});
822+
});
790823
});
791824

792825
describe('in DSL', function() {

0 commit comments

Comments
 (0)