Karma is test runner, that means that it can execute our tests which we wrote in Jasmine, for example.
First to install it, we need to install node.js. Installing is trivial, so I will not explain it, except that I needed to restart my computer, otherwise npm is not recognized...
Open command prompt and go to folder where your source is located, in my case that is C:\myProjects\js\myPhotoAlbum, you can see the project on my GitHub. Then Install karma:
npm install -g karma --save-dev
In my case I also installed karma command line interface:
npm install -g karma-cli --save-dev
Then, in root folder of my project I added karma config file (karma.config.js):
/*global module*/ module.exports = function(config) { "use strict"; config.set({ plugins: ['karma-chrome-launcher', 'karma-coverage', 'karma-jasmine'], frameworks: ['jasmine'], files: [ 'js/sm.namespaces.js', 'js/sm.googleMaps.js', 'js/sm.readEXIFdata.js', 'js/sm.visualize.js', 'js/start.js' ], // coverage reporter generates the coverage reporters: ['progress', 'coverage'], browsers: ['Chrome'], singleRun: true, preprocessors: { // source files, that you wanna generate coverage for // do not include tests or libraries // (these files will be instrumented by Istanbul) 'js/**/*.js': ['coverage'] }, // optionally, configure the reporter coverageReporter: { type : 'html', dir : 'coverage/' } }); };
Here notice line:
plugins: ['karma-chrome-launcher', 'karma-coverage', 'karma-jasmine']
this means that we have to install:
karma-chrome-launcher
npm install karma-chrome-launcher --save-dev
karma-coverage:
npm install karma-coverage --save-dev
and karma-jasmine:
npm install karma-jasmine --save-dev
Also, if we add package file (package.json):
{ "name": "myPhotoAlbum-Karma", "version": "0.0.1", "devDependencies": { "karma": "^0.12.31", "karma-chrome-launcher": "^0.1.7", "karma-coverage": "^0.2.7", "karma-jasmine": "^0.3.5", "karma-cli": "^0.0.4" } }
Then all we have to do is:
npm install
and all our packages which we defined in our package.json will be added. More about package.json you can read here.
Then notice line:
singleRun: true
with that line we said that we want our test to be executed only once. Now we can execute Karma:
karma start karma.conf.js
After executing Karma you can see folder:
C:\myProjects\js\myPhotoAlbum\coverage\Chrome 40.0.2214 (Windows 8.1)
and if you open index.html you should see something like: