Testing AngularJS with Jasmine & Karma

Last modified: July 11, 2017

Unit testing is a cornerstone of test driven development (TDD). While JavaScript has traditionally used Jasmine for unit testing, newer js frameworks have developed more advanced solutions for testing client side code.

Angular is one of these frameworks. While Jasmine remains the preferred framework for testing Angular apps, the creators of Angular have developed their own test runner called Karma. Karma runs on Node.js and gives developers the ability to run unit tests across multiple browsers and devices.

In this tutorial, we will quickly demonstrate how to use jasmine with karma to test your Angular.js app.

Getting Started

This tutorial assumes that you are familiar with the basics of Angular and Node. We will be creating a very simplistic Angular.js controller for testing purposes. To start, let's install the required dependencies through npm.

npm install karma karma-jasmine jasmine-core angular angular-mocks --save

In the example above, we are installing all of the necessary dependencies for our test runner. This includes karma, the jasmine plugin for karma (karma-jasmine), the jasmine core library (jasmine-core), as well as angular and angular-mocks (more on this later).

Configuring your Angular App for Karma

Now that we have all of the necessary dependencies, it's time to configure our app for the Karam test runner. Similar to npm's npm init, we can run:

karma init

You should now be prompted with a series of questions (similar to running npm init). While this gives you the opportunity to specify browsers, test frequencies, etc. you can ignore this for now by simply hitting return until the prompts stop.

If all went according to plan, you should now have a fully generated karma.conf.js file in your root directory.

Finally, navigate to the root of your project and run:

node_modules/karma/bin/karma start

This should start the Karma server on http://localhost:9876/ (or whichever port the server is running on. Check the console output for the correct address).

Writing a test spec

Now that you're up and running, it's time to start testing your app. In your root directory, add a file called test.spec.js and add the following:


describe('my controller', function(){
it('runs a test', function(){

This is the jasmine framework in a nutshell. Notice how we have defined a describe clause with a nested it clause. This aligns with Jasmine's behavioral driven development (BDD) approach. By keeping things verbose, desired behaviors are clearly defined and our test suite is more easily readable.

Running our spec

Now that we have our test.spec.js file ready to run, we need to configure our karma.conf.js file to include and run our spec. In the karma.conf.js file, you should see something like:

files: [],
singleRun: false

If the singleRun attribute is set to false then the test runner will automatically rerun the test suite each time a file is changed. This makes it very convenient for developers and is recommended.

The files attribute is an empty array. This array simply lists all of the files to run when we call karma start. With that said, we need to add the test spec file we just created so karma knows to run it.

files: ['test.spec.js']

By adding the file's string path to this array, Karma now knows to include our test spec when it runs. NOTE: File paths may differ depending on where you place your files. Be sure to correctly reference the spec's path in your karma config file. Now run node_modules/karma/bin/karma start, navigate to the karma server in your browser, and you should see your spec run in the terminal.

Testing Angular Components

Our test spec is a great example, but it lacks relevance to our Angular app. First of all, expecting '0' to be '0' isn't very revealing to our app's functionality. Also, we want to test actual Angular controllers, services, etc. that we've written.

To do this, we use angular-mocks: a library for mocking up our existing angular code so that we can use them in our spec files. You should already have angular-mocks installed via npm (see previous steps) but you also need to include this in your karma configuration. Head back to your karma.conf.js file and add the following paths:

files: [

Here we've included the Angular.js library as well as the angular-mocks module for mocking up our Angular components. Remember that ORDER MATTERS. We must include our Angular libraries before our test spec so we can reference everything.

You may notice that we don't have a controllers/myCtrl.js file yet. While you can reference any controller, service, etc. to test, we will create a simple controller for the sake of this example:


.controller('myCtrl', function($scope){
$scope.count = 0
$scope.add = function(){
$scope.count += 1;

Again, this is just a basic controller for our example's sake. Let's update our test spec to actually test our newly created controller.


describe('my controller', function(){
var $controller

$controller = _$controller_;

describe('$scope', function(){
it('adds one', function(){
var $scope = {};
var controller = $controller('myCtrl', {$scope: $scope});

In the above example, we use angular-mock to include our 'myModule' module with beforeEach(angular.mock.module('myModule')). We then inject an Angular controller before each test. Finally, in our describe clause, we initiate a new 'myCtrl' controller and bind it to our $scope variable. Then we can execute our $scope.add() function and test the result with an assertion (our expect clause).


If you've done everything right then your test should already be passing. Remember that we configured our karma.conf.js file to allow continuous runs. As next steps, start exploring further configuration options available for your Karma test runner. Also remember that other built-in Angular services (such as $httpBackend) are good for mocking up http requests and other asynchronous functionality. By using Karma and angular-mock, you can efficiently write unit tests for your Angular app.


August 2, 2017
great read ..thanks again team stackchief! keep them coming.
August 2, 2017
i agree with stackchief. karma is so prevalent within the Angular ecosystem. regardless of what version you are using these days you should still know the basics of configuring and writing unit tests with karma (especially if you want a job as Angular developer professionally).
August 2, 2017
karma makes testing angular apps so simple. although this article discusses testing Angular 1 apps with Karma, it should be noted that Angular 2+ automatically includes karma for unit testing. it is automatically included with Angular CLI build tools and is fundamental to the testing process with the Angular framework. EVEN IF YOU HAVE MOVED ON FROM ANGULAR 1 YOU WILL STILL USE KARMA! :)
Avery Thomas
June 20, 2017
angular-mock and karma make testing Angular super simple. it fits well into the node ecosystem as well.
Brenden U.
May 11, 2017
karma adds a lot of benefits, but i am old school and like to rely on jasmine alone. it's all javascript and it all runs through a browser. jasmine has been around for years and is a great way to test your js code. i think karma adds a lot of convenience to the testing process and makes it a lot easier to mock up dependencies etc, but by no means do you need karma to test.
Sarah M.
May 1, 2017
thanks for the quick tutorial. great read.
April 7, 2017
i think testing with good old jasmine is fine. tough to replicate / mimick some of the things going on with Angular though....but then again i hate all js frameworks
Sam Erickson
March 18, 2017
if you use angular and you haven't used karma, it's time to get started. can't think of another way to properly test Angular controllers that rely on a browser to run.
Andrea Karland
March 16, 2017
karma is essential to testing Angular apps. I would highly recommend getting started with it ASAP if you haven't already. i can't think of a better way to unit test Angular code anyways...
Mary Clover
March 12, 2017
Karma is neat since it runs your tests against a browser. Makes sense when testing JavaScript right?!
Terry Joiner
February 9, 2017
no errors, nice this helped a lot!
James Erickson
February 9, 2017
i think this is a great read for someone just getting started. Karma is crucial to unit testing Angular apps!
John G
February 9, 2017
good read

You might also like: