7
votes

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

2
Could you add your karma.conf.js code and also your project directory structure? - glepretre
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
@glepretre: Updated my question. - gelly
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
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

2 Answers

1
votes

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.
1
votes

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'
],