Configuring karma for AngularJS testing

I've been working on the test suite for ngLazy, and I thought it could be helpful to document some notes that seem relevant after setting up karma in this project.

First thing's first:
npm install karma

followed by:
karma init

The karma tests are configured via a file that is named karma.conf.js by convention. In it, we find a number of config properties on an object passed to the config.set method (config is the parameter that is passed to karma at the time of invocation). A little clarification about these properties would have helped quite a bit, namely:

  {
    files: [...],
  }

This files array must contain paths for all scripts that the app you are testing is dependent on. Not just your spec files. That means angular and all your other vital dependencies, including in this case, the path to ngLazy.

There is also an exclude:[...] property, so that you can include files above using a wildcard * and then exclude specific files within those directories.

Karma launches its own webserver, but this must run CONCURRENT to the locally served app you are testing. By default, it uses port 9876. But this is an important point to understand. You must also serve your app BEFORE you run tests and launch the karma server.

Running your tests will be simplified with a build tool like grunt, and the grunt-karma npm package. This will run the necessary karma commands after defining a karma step in your grunt test script. If you don't use grunt, that is cool too! But don't forget, before you run your tests, you must start the karma server:

karma start path/to/tests/karma.conf.js

Then, to run the tests:

karma run path/to/tests/karma.conf.js