Permalink
Browse files

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
  • Loading branch information...
1 parent 2ece1c9 commit 0baa17a3b7ad2b242df2b277b81cebdf75b04287 @shahata shahata committed with petebacondarwin Nov 30, 2014
Showing with 54 additions and 3 deletions.
  1. +2 −2 src/auto/injector.js
  2. +14 −0 src/ngMock/angular-mocks.js
  3. +5 −1 test/auto/injectorSpec.js
  4. +33 −0 test/ngMock/angular-mocksSpec.js
@@ -800,7 +800,7 @@ function createInjector(modulesToLoad, strictDi) {
}
var args = [],
- $inject = annotate(fn, strictDi, serviceName),
+ $inject = createInjector.$$annotate(fn, strictDi, serviceName),
length, i,
key;
@@ -839,7 +839,7 @@ function createInjector(modulesToLoad, strictDi) {
invoke: invoke,
instantiate: instantiate,
get: getService,
- annotate: annotate,
+ annotate: createInjector.$$annotate,
has: function(name) {
return providerCache.hasOwnProperty(name + providerSuffix) || cache.hasOwnProperty(name);
}
@@ -2127,18 +2127,32 @@ angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) {
if (window.jasmine || window.mocha) {
var currentSpec = null,
+ annotatedFunctions,
isSpecRunning = function() {
return !!currentSpec;
};
+ angular.mock.$$annotate = angular.injector.$$annotate;
+ angular.injector.$$annotate = function(fn) {
+ if (typeof fn === 'function' && !fn.$inject) {
+ annotatedFunctions.push(fn);
@jeffbcross
jeffbcross Jan 30, 2015 Contributor

@shahata There was one internal test that errored on this line because annotatedFunctions was undefined. The cause was that the application created a separate injector to get one-off access to ngResource in a base class that was just used as a base class to other classes. We worked around it by not needing to create the injector at all, but I wonder if there's a legitimate bug here.

@shahata
shahata Feb 1, 2015 Contributor

@jeffbcross yeah, this would indeed throw if annotate is somehow called before the first beforeEach. Is this the case you experienced? It can be fixed simply by initializing this variable to be an empty array.

+ }
+ return angular.mock.$$annotate.apply(this, arguments);
+ };
+
(window.beforeEach || window.setup)(function() {
+ annotatedFunctions = [];
currentSpec = this;
});
(window.afterEach || window.teardown)(function() {
var injector = currentSpec.$injector;
+ annotatedFunctions.forEach(function(fn) {
+ delete fn.$inject;
+ });
+
angular.forEach(currentSpec.$modules, function(module) {
if (module && module.$$hashKey) {
module.$$hashKey = undefined;
@@ -238,7 +238,11 @@ describe('injector', function() {
it('should publish annotate API', function() {
- expect(injector.annotate).toBe(annotate);
+ expect(angular.mock.$$annotate).toBe(annotate);
+ spyOn(angular.mock, '$$annotate').andCallThrough();
+ function fn() {}
+ injector.annotate(fn);
+ expect(angular.mock.$$annotate).toHaveBeenCalledWith(fn);
});
});
@@ -787,6 +787,39 @@ describe('ngMock', function() {
expect(testFn.$$hashKey).toBeUndefined();
});
});
+
+ describe('$inject cleanup', function() {
+ function testFn() {
+
+ }
+
+ it('should add $inject when invoking test function', inject(function($injector) {
+ $injector.invoke(testFn);
+ expect(testFn.$inject).toBeDefined();
+ }));
+
+ it('should cleanup $inject after previous test', function() {
+ expect(testFn.$inject).toBeUndefined();
+ });
+
+ it('should add $inject when annotating test function', inject(function($injector) {
+ $injector.annotate(testFn);
+ expect(testFn.$inject).toBeDefined();
+ }));
+
+ it('should cleanup $inject after previous test', function() {
+ expect(testFn.$inject).toBeUndefined();
+ });
+
+ it('should invoke an already annotated function', inject(function($injector) {
+ testFn.$inject = [];
+ $injector.invoke(testFn);
+ }));
+
+ it('should not cleanup $inject after previous test', function() {
+ expect(testFn.$inject).toBeDefined();
+ });
+ });
});
describe('in DSL', function() {

0 comments on commit 0baa17a

Please sign in to comment.