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 784ea8e commit 6ec5946094ee92b820bbacc886fa2367715e60b4 @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);
+ }
+ 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 6ec5946

Please sign in to comment.