Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-istanbul

istanbuljs80.8mBSD-3-Clause7.0.0

A babel plugin that adds istanbul instrumentation to ES6 code

istanbul, babel, plugin, instrumentation

readme

babel-plugin-istanbul

Coverage Status Conventional Commits community slack

Having problems? want to contribute? join our community slack.

A Babel plugin that instruments your code with Istanbul coverage. It can instantly be used with karma-coverage and mocha on Node.js (through nyc).

Note: This plugin does not generate any report or save any data to any file; it only adds instrumenting code to your JavaScript source code. To integrate with testing tools, please see the Integrations section.

Usage

Install it:

npm install --save-dev babel-plugin-istanbul

Add it to .babelrc in test mode:

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

Optionally, use cross-env to set NODE_ENV=test:

{
  "scripts": {
    "test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.js"
  }
}

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.

It has been tested with bemusic/bemuse project, which contains ~2400 statements.

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.

babel-plugin-istanbul respects the include/exclude configuration options from nyc, but you also need to configure NYC not to instrument your code by adding these settings in your package.json:

  "nyc": {
    "sourceMap": false,
    "instrument": false
  },

Ignoring files

You don't want to cover your test files as this will skew your coverage results. You can configure this by providing plugin options matching nyc's exclude/include rules:

{
  "env": {
    "test": {
      "plugins": [
        ["istanbul", {
          "exclude": [
            "**/*.spec.js"
          ]
        }]
      ]
    }
  }
}

If you don't provide options in your Babel config, the plugin will look for exclude/include config under an "nyc" key in package.json.

You can also use istanbul's ignore hints to specify specific lines of code to skip instrumenting.

Source Maps

By default, this plugin will pick up inline source maps and attach them to the instrumented code such that code coverage can be remapped back to the original source, even for multi-step build processes. This can be memory intensive. Set useInlineSourceMaps to prevent this behavior.

{
  "env": {
    "test": {
      "plugins": [
        ["istanbul", {
          "useInlineSourceMaps": false
        }]
      ]
    }
  }
}

If you're instrumenting code programatically, you can pass a source map explicitly.

import babelPluginIstanbul from 'babel-plugin-istanbul';

function instrument(sourceCode, sourceMap, filename) {
  return babel.transform(sourceCode, {
    filename,
    plugins: [
      [babelPluginIstanbul, {
        inputSourceMap: sourceMap
      }]
    ]
  })
}

Credit where credit is due

The approach used in babel-plugin-istanbul was inspired by Thai Pangsakulyanont's original library babel-plugin-__coverage__.

babel-plugin-istanbul for enterprise

Available as part of the Tidelift Subscription.

The maintainers of babel-plugin-istanbul and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

7.0.0 (2024-07-05)

⚠ BREAKING CHANGES

  • Drop support for Node versions 8 and 10

Bug Fixes

  • container is falsy error with block scoping transform (#291) (8e76919)
  • update istanbul-lib-instrument to v6 (#292) (643e080)

6.1.1 (2021-10-16)

Bug Fixes

  • build: first automated publication (9a1b893)

6.1.0 (2021-10-16)

Features

  • instrumenter: instrumenter with fixes for branchMap instrumentation (#265) (1e9ec62)

6.0.0 (2019-12-20)

⚠ BREAKING CHANGES

  • Drop node.js 6 (#226)

Features

5.2.0 (2019-07-18)

Bug Fixes

  • Print explicit error if use with babel 6 is attempted (#207) (a12cf16)

Features

  • Support turning off node_modules default exclude via flag (#172) (a314f06)

5.1.4 (2019-04-25)

5.1.3 (2019-04-23)

Bug Fixes

5.1.2 (2019-04-10)

Bug Fixes

  • Ensure correct scope references after traversal (#192) (201a933)

5.1.1 (2019-01-11)

Bug Fixes

5.1.0 (2018-09-07)

Features

5.0.1 (2018-07-24)

5.0.0 (2018-06-27)

Features

  • upgrade to babel 7 and newest istanbul libraries (#158) (a9e1564)

BREAKING CHANGES

  • Drop node 4 support, upgrade to babel 7, stop shipping @babel/plugin-syntax-object-rest-spread

4.1.6 (2018-03-09)

  • chore: explicit update of istanbul dependencies (#149) (77b6eb7), closes #149
  • chore(package): update coveralls to version 3.0.0 (#133) (7045a03), closes #133
  • chore(package): update mocha to version 4.0.0 (#134) (38176ba), closes #134
  • fix: babel-preset-es2015 => babel-preset-env (#138) (18fe954), closes #138
  • fix: include object-spread-syntax plugin (#141) (428a952), closes #141

4.1.5 (2017-08-23)

4.1.4 (2017-05-27)

4.1.3 (2017-04-29)

4.1.2 (2017-03-27)

4.1.1 (2017-03-22)

Bug Fixes

  • explicit upgrade to patched version of istanbul-lib-instrument (db8ecbe)

4.1.0 (2017-03-21)

Features

  • add includeUntested option to expose coverage of all instrumented files (#80) (b078bbd)

4.0.0 (2017-02-07)

Bug Fixes

  • load configuration from process.env.NYC_CONFIG if present (#93) (e902924)

Features

  • drop Node 0.10 and 0.12 support, upgrade dependencies to reflect this (#88) (594c03a)
  • we can now use the language feature Object.assign (#92) (f77db2a)

BREAKING CHANGES

  • I've updated dependencies and dropped Node 0.10 and Node 0.12 support.

3.1.2 (2017-01-04)

Bug Fixes

  • address regression related to export const foo = () => {} (#79) (f870a8f)

3.1.1 (2017-01-02)

3.1.0 (2016-12-27)

Bug Fixes

  • upgrade a bunch of core dependencies (#77) (e764330)

Features

  • accept source map input for the visitor (#75) (437e90b)

3.0.0 (2016-11-14)

Chores

  • package: update test-exclude to version 3.0.0 (#68) (0396385)

BREAKING CHANGES

  • package: test-exclude now adds **/node_modules/** as exclude rule by default.

2.0.3 (2016-10-17)

Bug Fixes

  • force istanbul-lib-instrument with variable hoisting fix (#64) (209a0cf)
  • switch deprecated lodash.assign for object-assign (#58) (6e051fc)

2.0.2 (2016-09-08)

Bug Fixes

  • take realpath of cwd, whether or not set in env (#37) (6274d83)

2.0.1 (2016-09-02)

Bug Fixes

  • update istanbul-lib-instrument (573e0d4)

2.0.0 (2016-08-14)

Chores

  • upgrade to version of test-exclude with new exclude rules (#35) (220ce2b)

BREAKING CHANGES

1.1.0 (2016-07-21)

Bug Fixes

  • upgrade to istanbul-lib-instrument with faster babel-generator (#18) (d33263c)

Features

  • allow exclude/include options to be passed as Babel plugin config (#16) (cf68421)

1.0.3 (2016-07-09)

Bug Fixes

1.0.2 (2016-07-03)

Bug Fixes

1.0.1 (2016-06-30)

Bug Fixes

  • upgrade to version of istanbul-lib-instrument that fixes some out of bounds issues (#6) (a949065), closes #6

1.0.0 (2016-06-26)

Features

  • port functionality from coverage, get ready for first release (#2) (2a8ee44)