Testing Angular with Karma, file ordering

I've been struggling with my karma file load order for a while now, this is the file for my karma.config.js:

files: [
            'bower/angular/angular.js',
            'node_modules/angular-mocks/angular-mocks.js',
            'src/**/*.module.js',
            'src/**/*.js',
            'tests/**/*.js'
        ],

      

The 2 files I really want to test are my pod module src/main/main.module.js

and my main pod controller src/main/main.controller.js

.

This is my test config (under tests/main/main.controller.spec.js

)

 var mockScope,
        controller,
        backend,
        mockInterval,
        mockTimeout;

beforeEach(angular.mock.module('main'));

beforeEach(angular.mock.inject(function ($controller, $rootScope, $http, $interval, $timeout) {
    mockScope = $rootScope.$new();
    mockInterval = $interval;
    mockTimeout = $timeout;
    controller = $controller('mainCtrl', {
        $scope: mockScope,
        $http: $http,
        $interval: mockInterval,
        $timeout: mockTimeout
    });
}));

      

As far as I know, this means the load order is as follows:

  • AngularJS
  • AngularJS dependent modules (angular-mocks)
  • My modules
  • My controllers, factories, directives, etc.
  • My test files

However, on startup, I get the following error: Error: [ng:areq] Argument 'mainCtrl' is not a function, got undefined

which is probably happening at the moment when I want to instantiate my controller in a test file.

The weird thing is that when I change the controller name to something that comes later main.module.js

in alphabetical order, like in xxx.controller.js

, everything works fine. According to the Karma documentation:

Multiple files matching the same pattern are sorted alphabetically.

This led me to believe that both my controller and module matched the pattern src/**/*.js

. However, the docs also state that:

Each file is included exactly once. If multiple patterns match the same file, it is included as if it matched only the first pattern.

So this shouldn't happen, my module matches both templates, but since the module template is listed above the controller template, it must match the previously declared template and therefore must be loaded before the controller.

When I check the tab on the net it was confusing me to find out that the load order is actually fine (as in module> controller> test) and is exactly the same as when I change the filename to whatever comes after the module ...

I am running karma from the root of my project with the following command: karma start karma.config.js

My karma version is 0.13.0

+3


source to share


3 answers


I just released v0.13.1

with a fix that should fix this problem, please let me know if it works for you.



+2


source


Like @hansmaad did, I eventually found that the glob pattern excludes the module from my controller pattern, so the change src/**/*.js

, 'src/**/!(*.module)*.js'

worked for me. Thought the earlier solution should also work according to the karma documentation. Perhaps this is a bug in karma 0.13.0?



0


source


try this way.

files: [
            'bower/angular/angular.js',
            'node_modules/angular-mocks/angular-mocks.js', 
            'src/**/*.js',
            'tests/**/*.js'
        ],

      

0


source







All Articles