Istanbul上的JSX测试的代码覆盖率 [英] Code coverage on JSX tests with Istanbul
问题描述
我正在尝试使用我的代码来获得一定的覆盖范围并运行,但是有些东西滑入了我的手指。
I am trying to instrument my code to get some coverage up and running, but something is slipping through my fingers.
我启动了 istanbul
与:
node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- -u exports -R spec
和我的 mocha.opts
看起来像这样:
app/assets/javascripts/components/**/*-mocha.jsx
--compilers jsx:mocha/compiler.js
一切似乎都运行良好(测试运行,至少),但是我唯一了解的是用于将JSX编译为JavaScript的文件(用于 compiler.js
Everything seems to run fine (the tests run, at least), but the only coverage that I get is on the files used to compile the JSX to JavaScript (used in compiler.js
compiler.js 100%
jsx-stub-transform.js 65%
非常有用...
有什么想法吗?
推荐答案
我正在使用摩卡和 Babel 6 的 https://github.com/douglasduteil/isparta rel = nofollow> isparta a>如下:
I am using mocha and isparta directly with Babel 6 as follows:
BABEL_ENV=test babel-node node_modules/isparta/bin/isparta cover _mocha
.babelrc
.babelrc
{
"plugins": [
"add-module-exports",
"transform-decorators-legacy",
"transform-runtime"
],
"presets": [
"es2015",
"react",
"stage-0"
],
"env": {
"test": {
"plugins": [
"rewire"
]
}
}
}
test / mocha.opts
test/mocha.opts
--compilers js:babel-core/register
--require test/init.js
src/**/*_test.js*
test.init.js
test.init.js
'use strict';
require('mock-require')('clappr');
require('testdom')('<html><body></body></html>', {
React: 'react',
localStorage: 'localStorage'
});
.istanbul.yml
.istanbul.yml
instrumentation:
root: src
excludes: ['*_test.js']
从 package.json
选择依赖项h2>
select dependencies from package.json
"babel-cli": "^6.7.5",
"babel-core": "^6.7.2",
"babel-eslint": "^5.0.0",
"babel-loader": "^6.2.4",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-rewire": "^1.0.0-rc-2",
"babel-plugin-runtime": "^1.0.7",
"babel-plugin-syntax-jsx": "^6.5.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.6.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.7.2",
"babel-runtime": "^5.8.34",
"babel-template": "^6.7.0",
"babel-types": "^6.7.2",
"isparta": "^4.0.0",
"mocha": "^2.4.5",
.JSX文件的注释
我将所有我的.JSX文件转换为.JS,原因如下:
A note on .JSX files
I renamed all of my .JSX files to .JS, and here's why:
- 我使用 codecov 用于托管覆盖率报告。这暴露了一个事实,尽管
coverage / lcov-report / index.html
显示了正确的覆盖率,但.JSX文件缺少JSON覆盖率文件中的某些内容。我从来没有想过。据我所知,这是isparta或istanbul中的错误。我还尝试了istanbul@1.0.0-alpha.2
,发现它也存在相同的问题。 - 用于推荐命名文件的反应.JSX,有利于转换实用程序和编辑器。这不再是建议。据我所知,这不再重要。
- I use codecov for hosted coverage reporting. This exposed the fact that while
coverage/lcov-report/index.html
showed the correct coverages, something in the JSON coverage files is missing for .JSX files. I was never able to figure it out. As far as I can tell, it's a bug in isparta or istanbul. I also triedistanbul@1.0.0-alpha.2
and found it had the same issue. - React used to recommend naming files .JSX for the benefit of transform utilites and editors. This is no longer a recommendation. As far as I can tell, it just doesn't matter anymore.
自从切换到.JS后,我再也没有看到
Since switching to .JS, I haven't seen any issues with tools including Atom and IntelliJ.
如果不想重命名文件,可以在上面的示例中添加以下内容:
If you don't want to rename your files, you can add the following to my examples from above:
- 在脚本中,在
isparta封面
之后,添加-include \ src / ** / * _ test.jsx\
。 - 至
.istanbul.yml
,添加
- In the script, after
isparta cover
, add--include \"src/**/*_test.jsx\"
. - To
.istanbul.yml
, add
extensions:
- .js
- .jsx
这篇关于Istanbul上的JSX测试的代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!