Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
with
54 additions
and 3 deletions.
- +2 −2 src/auto/injector.js
- +14 −0 src/ngMock/angular-mocks.js
- +5 −1 test/auto/injectorSpec.js
- +33 −0 test/ngMock/angular-mocksSpec.js
@@ -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); | ||
This comment has been minimized.
This comment has been minimized.
shahata
Author
Contributor
|
||
} | ||
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; | ||
@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.