title: Testing Angular with karma, file ordering
body: <p>I've been struggling with my karma file load order for a while now, this is the file config for my karma.config.js:</p>
  
  <pre><code>files: [
              'bower/angular/angular.js',
              'node_modules/angular-mocks/angular-mocks.js',
              'src/**/*.module.js',
              'src/**/*.js',
              'tests/**/*.js'
          ],
  </code></pre>
  
  <p>The 2 files I actually want to test are my module under <code>src/main/main.module.js</code> and my main controller under <code>src/main/main.controller.js</code>.</p>
  
  <p>This is my test config (under <code>tests/main/main.controller.spec.js</code>)</p>
  
  <pre><code> 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
      });
  }));
  </code></pre>
  
  <p>To the best of my knowledge this means that the loading order is as follows:</p>
  
  <ol>
  <li>AngularJS</li>
  <li>AngularJS dependent modules (angular-mocks)</li>
  <li>My modules</li>
  <li>My controllers, factories, directives, etc...</li>
  <li>My test files</li>
  </ol>
  
  <p>However when running this I get the following error: <code>Error: [ng:areq] Argument 'mainCtrl' is not a function, got undefined</code> which probably happens at the moment I want to instantiate my controller in my test file.</p>
  
  <p>The weird thing is, that when I change the controller name to something that comes later than <code>main.module.js</code> alphabetically, for example to <code>xxx.controller.js</code> everything works just fine. According to the karma documentation: </p>
  
  <blockquote>
    <p>Multiple files matching a single pattern are sorted alphabetically.</p>
  </blockquote>
  
  <p>This led me to believe that both my controller and module got matched to the pattern <code>src/**/*.js</code>. However the docs also state that:</p>
  
  <blockquote>
    <p>Each file is included exactly once. If multiple patterns match the same file, it's included as if it only matched the first pattern.</p>
  </blockquote>
  
  <p>So this shouldn't even happen, my module matches both patterns but since the module pattern is listed above the controller pattern it should match the earlier declared pattern and therefore be loaded before the controller. </p>
  
  <p>When I check the network tab it baffled me to find out that the load ordering is actually fine (as in module > controller > test) and is completely the same as when I change the filename to something that comes after the module.</p>
  
  <p>I run karma from the root of my project with the following command: <code>karma start karma.config.js</code> </p>
  
  <p>My karma version is 0.13.0</p>