摩卡+伊斯坦布尔+通天塔 [英] Running Mocha + Istanbul + Babel
问题描述
在使用Mocha和babel编译器运行istanbul时遇到一些问题.
I'm having some issues while running istanbul with mocha and the babel compiler..
我所有的测试都运行良好,但是在完成所有测试后,它向我显示以下消息:
all my tests are runnning just fine, but after all the tests done it shows me this message:
No coverage information was collected, exit without writing coverage information
并且它没有生成任何覆盖率报告.
And it is not producing any coverage report..
我正在运行的命令是:
NODE_ENV=test istanbul cover _mocha -- --require babel-core/register --recursive
该项目托管在github中: https://github.com/weslleyaraujo/react-flux-puzzle/tree/feat/unit-tests-24
the project is hosted in github: https://github.com/weslleyaraujo/react-flux-puzzle/tree/feat/unit-tests-24
有什么想法吗?
推荐答案
使用Babel 6.x,假设我们有文件test/pad.spec.js
:
Using Babel 6.x, let's say we have file test/pad.spec.js
:
import pad from '../src/assets/js/helpers/pad';
import assert from 'assert';
describe('pad', () => {
it('should pad a string', () => {
assert.equal(pad('foo', 4), '0foo');
});
});
安装一堆废话:
$ npm install babel-istanbul babel-cli babel-preset-es2015 mocha
创建一个.babelrc
:
{
"presets": ["es2015"]
}
运行测试:
$ node_modules/.bin/babel-node node_modules/.bin/babel-istanbul cover \
node_modules/.bin/_mocha -- test/pad.spec.js
pad
✓ should pad a string
1 passing (8ms)
=============================================================================
Writing coverage object [/Volumes/alien/projects/forked/react-flux-puzzle/coverage/coverage.json]
Writing coverage reports at [/Volumes/alien/projects/forked/react-flux-puzzle/coverage]
=============================================================================
=============================== Coverage summary ===============================
Statements : 100% ( 4/4 )
Branches : 66.67% ( 4/6 ), 1 ignored
Functions : 100% ( 1/1 )
Lines : 100% ( 3/3 )
================================================================================
更新:我已经成功使用nyc
(消耗istanbul
)而不是istanbul
/babel-istanbul
了.这有点复杂.尝试一下:
UPDATE: I've had success using nyc
(which consumes istanbul
) instead of istanbul
/babel-istanbul
. This is somewhat less complicated. To try it:
安装内容(您可以删除babel-istanbul
和babel-cli
):
Install stuff (you can remove babel-istanbul
and babel-cli
):
$ npm install babel-core babel-preset-es2015 mocha nyc
如上所述创建.babelrc
.
执行此操作:
$ node_modules/.bin/nyc --require babel-core/register node_modules/.bin/mocha \
test/pad.spec.js
...这应该会给您类似的结果.默认情况下,它将覆盖范围信息放入.nyc-output/
,并在控制台中打印漂亮的文本摘要.
...which should give you similar results. By default, it puts coverage info into .nyc-output/
, and prints a nice text summary in the console.
注意:将命令放在package.json
的scripts
字段中时,可以从任何这些命令中删除node_modules/.bin/
.
Note: You can remove node_modules/.bin/
from any of these commands when placing the command in package.json
's scripts
field.
这篇关于摩卡+伊斯坦布尔+通天塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!