7

I'm writing Jasmine tests to my Angularjs app. I generated karma.conf.js using karma init but when I run karma start i get warnings like this:

WARN [web-server]: 404: /bower_components/angular/angular.js
WARN [web-server]: 404: /js/app.js

karma.conf.js is in my app folder, which is the place for the bower_components folder as well.

I think maybe that could be because of my local test server where I'm using this approach: https://github.com/mhevery/angular-node-socketio

(I've been able to set up the tests like this in other project without a test server)

Can anybody please point me in the right direction here?


Update:

My karma.conf.js looks like this:

module.exports = function(config) {
  config.set({
    basePath: '.',
    frameworks: ['jasmine', 'requirejs'],
    files: [
      'tests/*.js',
      'js/*.js',
      'bower_components/angular/angular.js',
      'bower_components/angular-mocks/angular-mocks.js',
      'bower_components/angular-resource/angular-resource.js',
      'bower_components/d3/d3.js'
    ],
    exclude: [],
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: false
  });
};

Here's my directory structure:

enter image description here

||||||
  • 1
    Could you add your karma.conf.js code and also your project directory structure? – glepretre Feb 5 '14 at 15:45
  • Not a solution to your problem, but if you look at the angular seed project from the tutorial docs.angularjs.org/tutorial you get the environment installed, and it does work for testing. Run the server with the batch file/script and it will monitor your directory for changes and run the tests right away. – Steven Scott Feb 5 '14 at 16:46
  • @glepretre: Updated my question. – gelly Feb 6 '14 at 9:37
  • 1
    I think the problem is in your karma.conf.js basepath: could you try with '' (without dot)? You should also change logLevel: config.LOG_INFO, to logLevel: config.LOG_DEBUG, to see more info in the terminal. – glepretre Feb 6 '14 at 10:09
  • That helped a bit I think, however, now I get: " Uncaught ReferenceError: angular is not defined" in my app.js file. My app.js file looks like this: angular.module('App', [ 'ngResource', 'App.controllers', 'App.factories', 'App.directives', 'App.filters' ]); – gelly Feb 6 '14 at 10:28
1

Now that you've fixed the basepath (from '.' to '', see question comments above), you should change the order of files loading in your karma.conf.js :

module.exports = function(config) {
  config.set({
    basePath: '.',
    frameworks: ['jasmine', 'requirejs'],
    files: [
      //load angular.js first
      //(unless if you use jQuery which must be first if I remember well)
      'bower_components/angular/angular.js',
      //Then angular-modules
      'bower_components/angular-resource/angular-resource.js',
      'bower_components/angular-mocks/angular-mocks.js',
      //Other libs
      'bower_components/d3/d3.js',
      //Your app scripts
      'js/*.js',
      //And your specs 
      'tests/*.js'
    ],
    exclude: [],
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: false
  });
};

You can find more info here: http://karma-runner.github.io/0.10/config/files.html

Ordering

  1. The order of patterns determines the order of files in which they are included in the browser.
  2. Multiple files matching a single pattern are sorted alphabetically.
  3. Each file is included exactly once. If multiple patterns match the same file, it's included as if it only matched the first pattern.
||||||
  • Did you stop and restart karma? Karma cannot see karma config changes in autowatch. – glepretre Feb 6 '14 at 12:23
  • We are obviously missing something. I guess that your app works correctly (in browser). Which scripts do you load in your index.html head? – glepretre Feb 6 '14 at 12:34
  • In index.html I'm loading a file called commonScripts.js which uses document.write to add the following scripts: angular.js, angular-mocks.js, angular-resource.js, d3.js, app.js, controllers.js, filters.js, directives.js and factories.js. And yes, it works fine in browser. – gelly Feb 6 '14 at 13:00
  • Maybe the error comes from RequireJs? You do use it, right? – glepretre Feb 6 '14 at 13:32
  • let us continue this discussion in chat – glepretre Feb 6 '14 at 13:47
1

Your problem is probably the order you're loading your files in.

You may need to change the order to something like:

files: [
  'bower_components/angular/angular.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'bower_components/angular-resource/angular-resource.js',
  'bower_components/d3/d3.js',
  'js/*.js',
  'tests/*.js'
],
||||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.