Lim H.
Lim H.

Reputation: 10050

karma cannot load files injected by requirejs

I'm having troubles getting a simple karma test to run. I have the following code structure:

js/
  |-- tests.js
  |-- karma.config.js
  |-- app/
         |-- controllers.js
  |-- tests/
         |-- unit/
                 |-- loginSpec.js
  |-- vendor/
         |-- jquery.js

I'm following the documentation at http://karma-runner.github.io/0.8/plus/RequireJS.html and have my configuration set up as follow (minus the unimportant parts):

// base path, that will be used to resolve files and exclude
basePath = '';

// list of files / patterns to load in the browser
files = [
    JASMINE,
    JASMINE_ADAPTER,
    REQUIRE,
    REQUIRE_ADAPTER,
    'tests.js',
    {pattern: 'tests/unit/*.js', included: false}
];

In my controllers.js, I define a function called LoginCtrl and I want to test this function in loginSpec.js

define(['controllers'],function(controllers) {
    describe('Login controllers', function() {
        describe('LoginCtrl', function(){
            it('should return 1', function() {
                var scope = {},
                ctrl = new LoginCtrl(scope);
                expect(1).toBe(1);
            });
        });
    });
});

The problem is my browser cannot load the controllers.js file eventhough I've set up my main test file's requirejs configuration (tests.js) as follow:

var tests = Object.keys(window.__karma__.files).filter(function (file) {
      return /Spec\.js$/.test(file);
});
requirejs.config({
    baseUrl: '/base/app',
    paths: {
        jquery: 'vendor/jquery',
    },
    deps: tests,
    callback: window.__karma__.start
});

The browser does look for a file at http://localhost:9876/base/app/controllers.js. Isn't this the right path?

Upvotes: 3

Views: 1343

Answers (2)

inf3rno
inf3rno

Reputation: 26139

The path is not okay I think, it should be jquery: '../vendor/jquery', because the requirejs base points to the app dir. But this is not the only problem...

By karma you should add every file to the patterns you want to use. The files with the flag include: true will be runned by karma, the others can be used by tests. If two patterns cover a file name then the first pattern will override the second (so this is in reverse order than we usually do). In your case you should use something like this as karma.conf.js:

module.exports = function (config) {
    config.set({
        basePath: './',
        frameworks: ['jasmine', 'requirejs'],
        files: [
            {pattern: 'tests.js', included: true},
            {pattern: 'test/**/*.js', included: false},
            {pattern: 'app/**/*.js', included: false},
            {pattern: 'vendor/**/*.js', included: false}
        ],
        exclude: [

        ],
        reporters: ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Firefox'],
        captureTimeout: 6000,
        singleRun: false
    });
};

Upvotes: 1

user3167526
user3167526

Reputation: 11

I had the similar problem, but I didn't find nice solution. I had to apply "hack" by adding .js extension to in my spec file, in your case try to change 'controllers' to 'controllers.js' in your loginSpec.js

Upvotes: 0

Related Questions