angularjskarma-jasminegruntfile

Grunt-Karma-Jasmine: can not find module


I am trying to set up tests for my Angular.js project and I keep getting "$injector:nomod, Module 'result' is not available! You either misspelled..." error. I am sure that I am including "result" module in the "files" array inside "karma.config.js", basically it looks like this:

files: [
          '../javascripts/jquery-2.1.4.min.js',
          '../jquery-ui/jquery-ui.min.js',
          '../D3/d3.js',
          'libs/angular.min.js',
          'libs/angular-route.min.js',
          'libs/angular-animate.min.js',
          'libs/selectize.js',
          'libs/angular-selectize.js',
          'libs/angular-mocks.js',

          'simulator.js',

          '*.js',
          'services/**/*.js',
          'qa/tests-*.js'
],

...

I thought initially that the ordering of the main module: 'simulator' (defined inside 'simulator.js' file) is wrong, so I specifically moved it upwards, before the other modules, like the following stackoverflow thread recommends:

Angular module not available in Karma Jasmine test run

It did not help. Then I tried to make sure that the files are imported in the same order as in my angular apps' main entry file (except for angular-mocks.js and qa/tests-*.js), importing each single file, instead of using wildcards, but no success.

Jasmine definitely goes inside the test files but stumbles upon the line where I am trying to import the module "result":

describe('simulator.chartService', function() {

var chartService;
var graphConfig;

console.log("instantiating module result");
  beforeEach(module('result'));
console.log("finished instantiating");

beforeEach(inject(function($injector) {
  graphConfig = $injector.get('graphConfig');
  chartService = $injector.get('chartService');

}));

it('should be created', function() {
  expect(chartService.calcColors(10)).not.toBeNull();
});

});

So, I see that the error happens in-between two console.log() statements.

I suspect that still something can be wrong with the ordering of my files inside the array "files" in "karma.config.js". I have main module "simulator" which is dependent on other modules:

angular.module('simulator', ['ngRoute','ngAnimate','selectize','newexp2','newexp','login','edit','exps', 'result','templates','commons'])

Modules 'newexp2', 'newexp', 'login', 'edit', 'exps', 'result', 'templates' are all dependent on the module 'commons'.

How to correctly import interdependent modules inside the "files" array? Is it just enough to place "simulator.js", main module, above all others, or I also need to place all other modules before "commons.js"?

Another my suspicion is that angular.js library version that I downloaded from the official angular website, "angular-mocks.js", can be incompatible with other modules that I am using. I had such an issue with "angular-animate.js" file before.

As long as I surround my test code with $(function(){...}) (and all other my modules ARE surrounded with it) it does not generate the error while importing the result module, so I start seeing two console.log() statements without an error in-between, however, this generates some unknown error which prevents me from invoking the it part at all, whereas when I do not surround it with $(function(){...}), the it test is invoked, but the module result import fails.

So far I am pretty much stuck and do not know where to move and what to try. Any suggestion would be greatly appreciated.


Solution

  • OK, I figured it out. The issue was that ALL of my angular code was enclosed inside $(function(){...}). The solution is to remove all of the $function(){...}), then reorder javascript imports inside the main entry .html file, and then all of the testing starts working good. The question might be better to mark as duplicate with:

    Angular document.ready() issue