使用Babel 6.x,假设我们有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 ignoredFunctions : 100% ( 1/1 )Lines : 100% ( 3/3 )================================================================================
更新 :我已经成功使用
nyc(消耗
istanbul)而不是
istanbul/
babel-istanbul。这有点复杂。尝试一下:
安装内容(您可以删除
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/,并在控制台中显示漂亮的文本摘要。
注意:
node_modules/.bin/将命令放在
package.json的
scripts字段中时,可以从其中任何命令中删除。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)