README.md 6.12 KB
Newer Older
lxyang committed
1
# babel-plugin-ry-istanbul-web
lxyang committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

[![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)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
[![community slack](http://devtoolscommunity.herokuapp.com/badge.svg)](http://devtoolscommunity.herokuapp.com)

_Having problems? want to contribute? join our [community slack](http://devtoolscommunity.herokuapp.com)_.

A Babel plugin that instruments your code with Istanbul coverage.
It 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)).

__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](#integrations) section.

## 新增目录istanlibment
此目录为插桩方法,日后优化时使用,现使用原版本文件
新增方法:增量插桩两种方式
Serve packjson启动增加配置到时候不需要可以更改IS_PITCHING_PILE为false即可
"serve": "cross-env IS_PITCHING_PILE=true;cross-env INCREMENT=true;cross-env BRANCH=origin/master; vue-cli-service serve --mode dev"
IS_PITCHING_PILE是否插桩
INCREMENT是否过滤增量文件
BRANCH需要对比的分支isPitchingPile为true时默认origin/master当ISPITCHINGPILE为false的时候可以不穿
第二种(第一种后期优化加上现在使用第二种如参方式)
 [
      babel-plugin-ry-istanbul,
      {
        extension: ['.js', '.vue'],
        instrmenttation: { IS_PITCHING_PILE: true, INCREMENT: true, BRANCH: 'origin/master' }
      }
    ]
 ]
33 34 35 36 37
 ## 新增全局传值方法
IS_PITCHING_PILE是否插桩
INCREMENT是否过滤增量文件
BRANCH需要对比的分支isPitchingPile为true时默认origin/master当ISPITCHINGPILE为false的时候可以不穿
全局配置:如.env.dev文件下增加全局变量插桩,babel-plugin-ry-istanbul同样可以取到对应值,如VUE全局变量前记得添加VUE_APP_前缀,其他则需要看使用框架是否需要添加前缀
lxyang committed
38 39
 ## 新增parse-diff
 新增parse-diff组件源码
lxyang committed
40 41 42 43 44
## Usage

Install it:

```
45
npm install --save-dev babel-plugin-ry-istanbul
lxyang committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
```

Add it to `.babelrc` in test mode:

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

Optionally, use [cross-env](https://www.npmjs.com/package/cross-env) to set
`NODE_ENV=test`:

```json
{
  "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](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.

It has been tested with [bemusic/bemuse](https://codecov.io/github/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`](https://github.com/bcoe/nyc), which will collect all the coverage report.

83
babel-plugin-ry-istanbul respects the `include`/`exclude` configuration options from nyc,
lxyang committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
but you also need to __configure NYC not to instrument your code__ by adding these settings in your `package.json`:

```js
  "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](https://github.com/bcoe/nyc#excluding-files):

```json
{
  "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](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.

## 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.

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

If you're instrumenting code programatically, you can pass a source map explicitly.
```js
137
import babelPluginIstanbul from 'babel-plugin-ry-istanbul';
lxyang committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

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

## Credit where credit is due

153
The approach used in `babel-plugin-ry-istanbul` was inspired by [Thai Pangsakulyanont](https://github.com/dtinth)'s original library [`babel-plugin-__coverage__`](https://github.com/dtinth/babel-plugin-__coverage__).
lxyang committed
154

155
## `babel-plugin-ry-istanbul` for enterprise
lxyang committed
156 157 158

Available as part of the Tidelift Subscription.

159
The maintainers of `babel-plugin-ry-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-ry-istanbul?utm_source=npm-babel-plugin-ry-istanbul&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)