Setting up Karma and Jasmine unit testing for an AngularJS app

There’s probably easier ways to do this (using angular-seed as a starting point?) but I like to get an understanding of what it takes to do something from scratch, so here’s some notes on adding karma and jasmine to an existing AngularJS project (leave me a comment with suggestions or corrections). My references to getting this working:

Assuming you already have node.js, use npm to install the karma and dependencies:
sudo npm install -g karma
sudo npm install -g karma-jasmine
sudo npm install -g karma-chrome-launcher
sudo npm install -g karma-cli

Karma config file – in the files section, list all dependent libraries, controllers being tested, and path/pattern to the test js to be executed:

[code language=”javascript”]
module.exports = function (config) {
basePath: ‘.’,
files: [
exclude: [
autoWatch: true,
frameworks: [
browsers: [
plugins: [

Starting karma:

karma start karma.conf.js

Example skeleton Jasmine test:

[code language=”javascript”]
describe(‘ExmapleCtrl’, function() {

var $controller;

// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;

describe(‘test suite name’, function() {
it(‘description of test’, function() {
var $scope = {};
var controller = $controller(‘ExampleCtrl’, { $scope: $scope });

//setup values
$scope.somevalue = 1;

//call method on controller

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.