I want to run sample test Angular code using Karma.
I try to modify above code to make sure, that controller will be injected to test. Unfortunetly, inject statement doesn't execute. I have Angular and Angular-mocks in version 1.0.8 and Node in version 8.16.2. I can't modify these versions, because my all project may to stop working.
Additionaly, I get error message TypeError: undefined is not an object (evaluating 'spec.$injector') in src/ProjectBundle/Resources/public/assets/vendor/bower_components/angular-mocks/angular-mocks.js (line 1715)
workFn@src/ProjectBundle/Resources/public/assets/vendor/bower_components/angular-mocks/angular-mocks.js:1715:15.
In Karma's configuration I have paths to Angular, Angular-mock, JavaScript code and test code. If I try to add path direct to test file (with word require), then test doesn't work.
Below is fragment of example file after modification.
beforeEach(inject(function(_$controller_, _$rootScope_){
console.info('test');
$controller = _$controller_;
$rootScope = _$rootScope_;
}));
Below is fragment of angular-mock.js file.
window.module = angular.mock.module = function() {
var moduleFns = Array.prototype.slice.call(arguments, 0);
return isSpecRunning() ? workFn() : workFn;
/////////////////////
function workFn() {
var spec = getCurrentSpec();
if (spec.$injector) {
throw Error('Injector already created, can not register a module!');
} else {
var modules = spec.$modules || (spec.$modules = []);
angular.forEach(moduleFns, function(module) {
modules.push(module);
});
}
}
};
Below is fragment of karma-ci.conf.js file.
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
plugins : ['karma-jasmine', 'karma-phantomjs-launcher'],
files: [
'src/ProjectBundle/Resources/public/assets/vendor/bower_components/angular/angular.js'
, 'src/ProjectBundle/Resources/public/assets/vendor/bower_components/angular-mocks/angular-mocks.js'
, 'src/ProjectBundle/Resources/public/assets/javascripts/cm/**/*.js' // contains above example file
, 'src/ProjectBundle/Tests/Karma/Unit/Controllers/AngularCtrlSpec.js' // contains above example test
],
browsers: ['PhantomJS']
});
};
Below is fragment of package.json file.
"devDependencies": {
"jasmine": "^3.6.3",
"jasmine-core": "^3.6.0",
"karma": "^5.2.3",
"karma-jasmine": "^4.0.1",
"karma-phantomjs-launcher": "^1.0.4"
}
Do you know any solution of my problem?