babel-plugin-coverage icon indicating copy to clipboard operation
babel-plugin-coverage copied to clipboard

Istanbul-compatible code coverage instrumentation plugin for Babel.

DEPRECATED! use babel-plugin-istanbul instead.

Note

This is a fork of babel-plugin-coverage by @dtinth, who did most of the work. This fork has a few changes on top of it:

  1. Supports istanbul-style ignore hints, aka /* istanbul ignore next */.

  2. Does not treat parts of conditionals and logical expressions as statements, which is an opinionated choice by babel-plugin-__coverage__. The behavior of this plugin is more akin to istanbul's original behavior.

Usage

Install it:

npm install --save-dev babel-plugin-coverage

Add it to .babelrc in test mode:

{
  "env": {
    "test": {
      "plugins": ["coverage"]
    }
  }
}

Integrations

karma

It just works with Karma. First, make sure that the code is already transpiled by Babel (either using karma-babel-preprocessor, karma-webpack, or karma-browserify). Then, simply set up karma-coverage according to the docs, but don’t add the coverage preprocessor. This plugin has already instrumented your code, and Karma should pick it up automatically.

mocha on node.js (through nyc)

Configure Mocha to transpile JavaScript code using Babel, then you can run your tests with nyc, which will collect all the coverage report. You need to configure NYC not to instrument your code by setting this in your package.json:

  "nyc": {
    "include": [ "/" ]
  },

Ignoring files

You don't want to cover your test files as this will skew your coverage results. You can configure this by configuring the plugin like so:

"plugins": [
  ["coverage", { "ignore": "test/" } ]
]

Where ignore is a glob pattern, or an Array of multiple patterns.

Alternatively, you can specify only which will take precedence over ignore:

"plugins": [
  ["coverage", { "only": "src/" } ]
]