westandy
westandy

Reputation: 1440

Karma Jasmine Angular Error: [$injector:nomod]

Short version: When I run karma, angular is telling me it cannot inject my 'common' module. The actual error is:

Error: [$injector:nomod] http://errors.angularjs.org/1.5.5/$injector/nomod?p0=common

Here's the details:

My Karma test.js

"use strict";

describe('Testing ModuleToTest',function(){
    beforeEach(module('ModuleToTest'));

    var $scope = {};
    var factory = {...make up stuff...}

    // inject - access angular controller 
    beforeEach(inject([
        '$controller','$rootScope',
        function($ctrl,$rs){
        $scope = $rs.$new();
        $ctrl('ModuleToTestCtrl',{
            $scope:$scope,
            CommonFactory:factory
        });
    }]));

    it('should define a someStuff object',function(){
        expect(testScope.someStuff).toBeDefined();
    });
});

My ModuleToTest definition:

module.js file:

angular.module('ModuleToTest',[]);

controller.js file:

angular.module('ModuleToTest')
.controller('ModuleToTestCtrl',
['$scope','$http','CommonFactory',
function($scope,factory){
    $scope.someStuff = ['foo','bar'];
}]);

The module that defines the CommonFactory:

module.js file:

angular.module('common',[]);

CommonFactory.js

angular.module('common')
.factory('CommonFactory',['$http',function($http){
   // do factory stuff here
}]);

Application module defintion, app.js:

angular.module('MyMainApp',['ModuleToTest', ... , 'common']);

karma.config.js:

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],//'requirejs'],


// list of files / patterns to load in the browser
files: [
  {pattern: 'js/vendor/angular/*.js'},
  {pattern: 'js/common/*.js'},
  {pattern: 'moduleToTest/js/*.js'},
  {pattern: 'moduleToTest/*.html'},
  {pattern: 'js/*.js'},
  {pattern: 'index.html'},
  {pattern: 'test/test.js'}
],


// list of files to exclude
exclude: [
  'js/vendor/bootstrap*.js',
  'js/vendor/jquery*.js'
],

Quick caveate: MyMainApp works as I expect it to. I am just beginning to use Karma to build unit tests for my applications modules. Eventually I will build the unit tests for MyMainApp, but I'm trying to start small.

Any suggestions or clarifications are welcome, in addition to answers! Thank you!

UPDATE: After helpful responses (thank you by the way!) In short, I'm getting the same error, but I've tried to simplify the problem.

I removed any mention of 'common' from my ModuleToTest. And Karma is still complaining about 'common'. Here's the details:

My Karma test.js

"use strict";

describe('Testing ModuleToTest',function(){
    var testScope = {};

    beforeEach(function(){

        module('ModuleToTest');

        inject(function($rootScope,$controller){
            testScope = $rootScope.$new();
            $controller('ModuleToTestCtrl',{
                $scope: testScope
            }); 
        });
    });

    // No it statement for now because the module won't even load
});

My ModuleToTest definition:

module.js file (notice no reference to CommonFactory):

angular.module('ModuleToTest',[]);

controller.js file (notice the removal of CommonFactory):

angular.module('ModuleToTest')
.controller('ModuleToTestCtrl',
['$scope',function($scope){
    $scope.someStuff = ['foo','bar'];
}]);

ANSWER: Given the simplified version, the solution was to put angular-mocks.js after angular.min.js in the karma.conf.js file.

I hope this saves somebody else the trouble.

Upvotes: 1

Views: 1242

Answers (1)

Phil
Phil

Reputation: 164739

You should have "common" as a dependency to "ModuleToTest" as that is where its components are used, ie

angular.module('ModuleToTest', ['common']);

For your test, you should really be creating a mock CommonFactory for complete collaborator isolation.

describe('Testing ModuleToTest', function() {
    var testScope, CommonFactory;

    beforeEach(function() {
        CommonFactory = {}; // or whatever mock implementation you need

        module('ModuleToTest');

        inject(function($rootScope, $controller) {
            testScope = $rootScope.$new();
            $controller('ModuleToTestCtrl', {
                $scope: testScope,
                CommonFactory: CommonFactory
            });
        });
    })

    // specs go here
});

Upvotes: 1

Related Questions