Setting up Karma to run unit tests in PhantomJS and Chrome

This session talks about setting up Karma to run the unit tests in PhantomJS and Chrome. The unit tests are written using Jasmine. The tests are written for an AngularJS application, however you can write unit tests for just about any JavaScript code.

If you are new to AngularJS, you might first want to watch my Getting to know AngularJS video.

Setting up Karma to run unit tests in PhantomJS and Chrome

Node packages to install:

npm install -g karma-cli
npm install -g karma --save-dev
npm install -g phantomjs
npm install -g karma-jasmine --save-dev
npm install -g karma-phantomjs-launcher --save-dev
npm install -g karma-chrome-launcher --save-dev

Install the last one only if you plan to run your unit tests using Chrome; you don’t need it if you prefer running them only in PhantomJS. Now, create the karma configuration file.

karma init karma2.conf.js

To run karma: karma start karma2.conf.js

For code coverage:

npm install -g karma-coverage

Make the changes as shown in the video and run karma as shown above.

Code sample here.

No Comments