Skip to content

Karma How To

Kitson Kelly edited this page Sep 26, 2015 · 2 revisions

This page answers the question of how to utilize remap-istanbul with Karma.

Installation

Your project will need karma, karma-coverage and remap-istanbul as development dependencies, so from the root path of your project:

$ npm install karma karma-coverage remap-istanbul --save-dev

Configuration

You will need to ensure that karma-coverage is configured appropriately for your project. In particular you need your coverage output as JSON:

// karma.conf.js
module.exports = function(config) {
  config.set({
    files: [
      'src/**/*.js',
      'test/**/*.js'
    ],

    // coverage reporter generates the coverage
    reporters: ['progress', 'coverage'],

    preprocessors: {
      // source files, that you wanna generate coverage for
      // do not include tests or libraries
      // (these files will be instrumented by Istanbul)
      'src/**/*.js': ['coverage']
    },

    // optionally, configure the reporter
    coverageReporter: {
      type : 'json',
      subdir : '.',
      file : 'coverage-final.json'
    }
  });
};

Usage

You should now be ready to go in order to invoke karma:

$ karma start karma.conf.js --single-run

This should output a coverage-final.json to the root of your project that is the coverage information for your built code. In order to have this remapped to the source code, you will then need to run remap-istanbul. From the command line, this would look like this:

$ node_modules/.bin/remap-istanbul -i coverage-final.json -o coverage-mapped.json

This would then output a ./coverage-mapped.json that has been remapped to your source files.

References

There are a lot more options and ways of using Karma and remap-istanbul and this How To is not exhaustive. Please refer to the appropriate documentation to better understand all the options available to you:

Clone this wiki locally