使用巴别塔6.x、 假设我们有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 )
================================================================================
UPDATE:我成功地使用了nyc
(消耗istanbul
),而不是istanbul
/babel-istanbul
.这就不那么复杂了.要try 它:
安装东西(可以删除babel-istanbul
和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/
,并在控制台中打印一个漂亮的文本摘要.
Note:将命令放入package.json
的scripts
字段时,可以从这些命令中删除node_modules/.bin/
.