@achil/babel-plugin-istanbul 中文文档教程
babel-plugin-istanbul
有问题? 想贡献? 加入我们的community slack。
一个 Babel 插件,可以用 Istanbul 覆盖率检测你的代码。 它可以立即与 karma-coverage 和 Node.js 上的 mocha 一起使用(通过 纽约)。
注意:此插件不会生成任何报告或将任何数据保存到任何文件; 它只会将检测代码添加到您的 JavaScript 源代码中。 要与测试工具集成,请参阅集成部分。
Usage
安装它:
npm install --save-dev babel-plugin-istanbul
在测试模式下将它添加到 .babelrc
:
{
"env": {
"test": {
"plugins": [ "istanbul" ]
}
}
}
可选地,使用 cross-env 设置 NODE_ENV=test
:
{
"scripts": {
"test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.js"
}
}
Integrations
karma
它只适用于 Karma。 首先,确保代码已经被 Babel 转译(使用 karma-babel-preprocessor
、karma-webpack
或 karma-browserify
). 然后,根据文档简单地设置karma-coverage,但是不要添加coverage
预处理器。这个插件已经检测了你的代码,Karma 应该会自动拾取它。
它已经通过 bemusic/bemuse 项目进行了测试,其中包含约 2400 条语句。
mocha on node.js (through nyc)
配置 Mocha 以使用 Babel 转译 JavaScript 代码,然后您可以使用 nyc
运行测试,它将收集所有的覆盖率报告。
babel-plugin-istanbul 遵循来自 nyc 的 include
/exclude
配置选项, 但您还需要通过在您的 package.json
中添加这些设置来配置 NYC 不检测您的代码:
"nyc": {
"sourceMap": false,
"instrument": false
},
Ignoring files
您不想覆盖您的测试文件,因为这会造成偏差您的报道结果。 您可以通过提供匹配 nyc 的 exclude
/include
规则的插件选项来配置它:
{
"env": {
"test": {
"plugins": [
["istanbul", {
"exclude": [
"**/*.spec.js"
]
}]
]
}
}
}
如果您没有在 Babel 配置中提供选项,插件将在 "nyc"exclude
/include
配置code> 键入 package.json
。
您还可以使用伊斯坦布尔的忽略提示< /a> 指定特定的代码行以跳过检测。
Source Maps
默认情况下,此插件将获取内联源映射并将它们附加到检测代码,以便代码覆盖率可以重新映射回原始源,即使对于多步骤构建过程也是如此。 这可能会占用大量内存。 设置 useInlineSourceMaps
来防止这种行为。
{
"env": {
"test": {
"plugins": [
["istanbul", {
"useInlineSourceMaps": false
}]
]
}
}
}
如果您以编程方式检测代码,则可以显式传递源映射。
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
babel-plugin-istanbul
中使用的方法受到 Thai Pangsakulyanont 的原始库 babel-plugin-__coverage__
。
babel-plugin-istanbul
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__
.
你可能也喜欢
- 4all-ui 中文文档教程
- @a-ignatov-parc/virtual-dom 中文文档教程
- @a-robu/victor 中文文档教程
- @aakash-goel/devonly-formatter 中文文档教程
- @abalita/ccci-vue-material 中文文档教程
- @abcnews/shrink-ray 中文文档教程
- @abdullahnasir/react-native-documents-viewer 中文文档教程
- @aboveyou00/markdown-it 中文文档教程
- @acala-network/eth-transactions 中文文档教程
- @accele/accele 中文文档教程