javascriptangularjsgulpkarma-runnerkarma-jasmine

Gulp + karma + jasmine + angular + singleRun:true = angular fails to inject


I have an issue when running Jasmine tests through Karma and Gulp, I'm running tests through a gulp task with

return new karma.Server({
    configFile: __dirname + '\\karma.conf.js',
    singleRun: true
}, done).start();

Which fails on my angular dependency injection, I get a standard angular unknown provider myProvider error. When I change the singleRun property to false, most of the unit tests pass (one set still fails though).

The tests have (and still) work fine in Chutzpah.

I've tried reducing my tests to the barebones:

var myProvider;
beforeEach(module('MyModule'));

beforeEach(inject(function (_my_) {
    myProvider = _my_;
}));

it("exists", function () {
    expect(myProvider).not.toBeUndefined();
});

and it still fails with the unknown provider myProvider error. Again, they run fine if I set singleRun to false. I've tried running PhantomJS and Chrome, they both show the exact same behavior.

I'm using angular 1.4.7 and my dependencies in package.json look like

  "dependencies": {
    "gulp": "^3.9.0",
    "gulp-chug": "^0.4.2",
    "gulp-install": "^0.6.0",
    "gulp-util": "^3.0.7",
    "phantomjs": "^1.9.19",
    "jasmine-core": "^2.4.1",
    "karma": "^0.13.19",
    "karma-jasmine": "^0.3.6",
    "karma-phantomjs-launcher": "^0.2.3",
    "karma-spec-reporter": "^0.0.23",
    "karma-chrome-launcher": "^0.2.2"
  }

karma.conf.js:

module.exports = function (config) {
    config.set({
        frameworks: ["jasmine"],
        reporters: ["spec"],
        browsers: ["PhantomJS"],
        files: [
            "./path/to/jquery/jquery-1.11.0.min.js",

            "./path/to/angular/angular.js",
            "./path/to/angular/angular-route.js",
            "./path/to/angular/angular-mocks.js",

            // I'm using this path order (app -> modules -> all) 
            // due to recommendations found on SO
            "./path/to/application/app.js",
            "./path/to/application/**/*Module.js",
            "./path/to/application/**/*.js",

            // For now, I'm just running the one test to simplify debugging
            "./path/to/tests/myTest.Test.js"
        ]
    });
};

Any ideas?


Solution

  • Turns out that our custom angular providers had internal dependencies that for some reason weren't being resolved with singleRun: true.

    I specifically set the load order for these files in karma.conf so that the dependencies load in the "correct" order and now I get the same result as when running with singleRun:false (the same few tests as with singleRun:false still fail, but that's another story).

    The reason this worked with Chutzpah was that those references (which were listed in each individual test file) were in the correct order.