6.1.1 • Published 2 years ago

babel-plugin-istanbul v6.1.1

Weekly downloads
12,032,878
License
BSD-3-Clause
Repository
github
Last release
2 years ago

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.

babel-jest@jest/transform@angular-devkit/build-angular@rpcbase/webpack-browserarchetype-library@instructure/ui-babel-preseteasy-select-rnreact-native-bluetooth2killi8n-react-native-fast-image@modevel/cli@rock-kit/ui-babel-presetspecify-importsbabel-specify-imports@icanpm/api-mastermui-table-reactrangewell-dataproviderararin-uiyxmf11111xb123456789xm-reactreact-native-template-rfbaseairscanairscan-examplereact-native-esc-pos-sahaaboperation-module1@borisovart/atol-kkt-module@nodutilus/nyc-config@moderno/workbenchdeneme323112@ntt_app/react-native-custom-notificationreact-native-covid-sdkgql_din_modbitgeteduifather-build-mbptest@cvpcasada/nwb@olivervorasai/sliderreact-native-printer-brotherstealess-helpers@aft/heisenbergreact-native-shekhar-bridge-test@service-exchange/buildwilscanner@oiti/documentoscopy-react-native@mink-opn/build-tokensquoc-testvbtreact-native-slider-kftest-react-scripts-cond@infinitebrahmanuniverse/nolb-babel-plugin-i@wumijs/cli@saaspe/componentsplginexpand-react-bridgeluminos-ui-coresklif-ui-kitsklif-api@everything-registry/sub-chunk-1202jawwy-sdkjawwy_gamification_release@314oner_npm/universal-components-libraryreact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkgp149-tablesklif-uireact-native-jawwy_sample@skedulr/nebula-ui-libraryabsurdly.js@alxrg/date-maker@angangii/frontendpracticeaxonv2sdk@amirdiafi/react-native-ios-haptics@angular-architects/build-angular@amjs/js-fw@amjs/react-tools@amjs/vue-toolsbabel-preset-blockaibabel-preset-bindedbabel-preset-closedintervalbabel-preset-rolo-backendbabel-preset-rolo-backend-tsbabel-preset-spaceshipbasebuild-component@arielapaula/components@arielapaula/test@aristidenf/streak-counter@asiz33/smartblok-vendure-plugin@assits/bro-buildbee-tools-test@ailjc/build@alexshmyrkov/react-ranger@alot/jest-presetbirken-react-native-community-image-editorbiginputzlgbiometric-stbf-build-toolsbgood-cli@ali5049/react-native-buttons
6.1.1

2 years ago

6.0.0

4 years ago

6.0.0-beta.2

4 years ago

6.0.0-beta.1

4 years ago

6.0.0-beta.0

4 years ago

5.2.0

5 years ago

5.1.4

5 years ago

5.1.3

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.1.6

6 years ago

4.1.5

7 years ago

4.1.4

7 years ago

4.1.3

7 years ago

4.1.1

7 years ago

4.0.0

7 years ago

3.1.2

7 years ago

3.1.1

7 years ago

3.0.0

7 years ago

2.0.3

7 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.0

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

1.0.0-alpha.1

8 years ago