UNPKG

5.21 kBMarkdownView Raw
1# babel-plugin-istanbul
2
3[![Greenkeeper badge](https://badges.greenkeeper.io/istanbuljs/babel-plugin-istanbul.svg)](https://greenkeeper.io/)
4[![Build Status](https://travis-ci.org/istanbuljs/babel-plugin-istanbul.svg?branch=master)](https://travis-ci.org/istanbuljs/babel-plugin-istanbul)
5[![Coverage Status](https://coveralls.io/repos/github/istanbuljs/babel-plugin-istanbul/badge.svg?branch=master)](https://coveralls.io/github/istanbuljs/babel-plugin-istanbul?branch=master)
6[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
7[![community slack](http://devtoolscommunity.herokuapp.com/badge.svg)](http://devtoolscommunity.herokuapp.com)
8
9_Having problems? want to contribute? join our [community slack](http://devtoolscommunity.herokuapp.com)_.
10
11A Babel plugin that instruments your code with Istanbul coverage.
12It can instantly be used with [karma-coverage](https://github.com/karma-runner/karma-coverage) and mocha on Node.js (through [nyc](https://github.com/bcoe/nyc)).
13
14__Note:__ This plugin does not generate any report or save any data to any file;
15it only adds instrumenting code to your JavaScript source code.
16To integrate with testing tools, please see the [Integrations](#integrations) section.
17
18## Usage
19
20Install it:
21
22```
23npm install --save-dev babel-plugin-istanbul
24```
25
26Add it to `.babelrc` in test mode:
27
28```js
29{
30 "env": {
31 "test": {
32 "plugins": [ "istanbul" ]
33 }
34 }
35}
36```
37
38Optionally, use [cross-env](https://www.npmjs.com/package/cross-env) to set
39`NODE_ENV=test`:
40
41```json
42{
43 "scripts": {
44 "test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.js"
45 }
46}
47```
48
49## Integrations
50
51### karma
52
53It _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](https://github.com/karma-runner/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.
54
55It has been tested with [bemusic/bemuse](https://codecov.io/github/bemusic/bemuse) project, which contains ~2400 statements.
56
57### mocha on node.js (through nyc)
58
59Configure Mocha to transpile JavaScript code using Babel, then you can run your tests with [`nyc`](https://github.com/bcoe/nyc), which will collect all the coverage report.
60
61babel-plugin-istanbul respects the `include`/`exclude` configuration options from nyc,
62but you also need to __configure NYC not to instrument your code__ by adding these settings in your `package.json`:
63
64```js
65 "nyc": {
66 "sourceMap": false,
67 "instrument": false
68 },
69```
70
71## Ignoring files
72
73You 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](https://github.com/bcoe/nyc#excluding-files):
74
75```json
76{
77 "env": {
78 "test": {
79 "plugins": [
80 ["istanbul", {
81 "exclude": [
82 "**/*.spec.js"
83 ]
84 }]
85 ]
86 }
87 }
88}
89```
90
91If you don't provide options in your Babel config, the plugin will look for `exclude`/`include` config under an `"nyc"` key in `package.json`.
92
93You can also use [istanbul's ignore hints](https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes) to specify specific lines of code to skip instrumenting.
94
95## Source Maps
96
97By 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.
98
99```json
100{
101 "env": {
102 "test": {
103 "plugins": [
104 ["istanbul", {
105 "useInlineSourceMaps": false
106 }]
107 ]
108 }
109 }
110}
111```
112
113If you're instrumenting code programatically, you can pass a source map explicitly.
114```js
115import babelPluginIstanbul from 'babel-plugin-istanbul';
116
117function instrument(sourceCode, sourceMap, fileName) {
118 return babel.transform(sourceCode, {
119 filename,
120 plugins: [
121 [babelPluginIstanbul, {
122 inputSourceMap: sourceMap
123 }]
124 ]
125 })
126}
127```
128
129## Credit where credit is due
130
131The approach used in `babel-plugin-istanbul` was inspired by [Thai Pangsakulyanont](https://github.com/dtinth)'s original library [`babel-plugin-__coverage__`](https://github.com/dtinth/babel-plugin-__coverage__).
132
133## `babel-plugin-istanbul` for enterprise
134
135Available as part of the Tidelift Subscription.
136
137The 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.](https://tidelift.com/subscription/pkg/npm-babel-plugin-istanbul?utm_source=npm-babel-plugin-istanbul&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)