javascript 完整的 Gulp 伊斯坦布尔覆盖报告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22702578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Full Gulp Istanbul Coverage Report
提问by Scott
I am using gulp-istanbul to generate JavaScript unit test coverage reports through Gulp. Is there a way to configure Istanbul to generate a full coverage report of all the JS files in my gulp stream, and not just the files touched by a test case.
我正在使用 gulp-istanbul 通过 Gulp 生成 JavaScript 单元测试覆盖率报告。有没有办法配置伊斯坦布尔以生成我的 gulp 流中所有 JS 文件的完整覆盖报告,而不仅仅是测试用例触及的文件。
I'm working on a project with a lot of JS, but no unit tests, and we are trying to increase the test coverage. I would like to have a coverage report that starts by show 0% coverage for most of our files, but over time will present an increasing coverage percentage.
我正在处理一个有很多 JS 但没有单元测试的项目,我们正在努力增加测试覆盖率。我想要一份覆盖率报告,首先显示我们大多数文件的覆盖率为 0%,但随着时间的推移,覆盖率会增加。
gulp.task( 'test', function () {
gulp.src( [ my source glob ] )
.pipe( istanbul() )
.on( 'end', function () {
gulp.src( [ my test spec glob ] )
.pipe( mocha( {
reporter: 'spec'
} ) )
.pipe( istanbul.writeReports(
[ output location ]
) );
} );
} );
回答by Romain Braun
It actually is much more simple now and you just have to add includeUntested
to your istanbul()
call.
现在它实际上要简单得多,您只需添加includeUntested
到您的istanbul()
电话中即可。
gulp.task('test', function () {
return gulp.src('./assets/**/js/*.js')
// Right there
.pipe(istanbul({includeUntested: true}))
.on('finish', function () {
gulp.src('./assets/js/test/test.js')
.pipe(mocha({reporter: 'spec'}))
.pipe(istanbul.writeReports({
dir: './assets/unit-test-coverage',
reporters: [ 'lcov' ],
reportOpts: { dir: './assets/unit-test-coverage'}
}));
});
});
Source : https://github.com/SBoudrias/gulp-istanbul#includeuntested
来源:https: //github.com/SBoudrias/gulp-istanbul#includeuntested
回答by bline
I've been struggling with this today and found no answers on Google. I finally came up with an answer very similar to an answer here but the writeReports call must go before the call to mocha.
我今天一直在努力解决这个问题,但在谷歌上没有找到答案。我终于想出了一个与这里的答案非常相似的答案,但 writeReports 调用必须在调用 mocha 之前进行。
gulp.task('test', ['build'], function(done) {
gulp.src('lib/**/*.js')
.pipe($.istanbul({ includeUntested: true }))
.on('end', function() {
gulp.src(['test/helpers/**/*.coffee', 'test/spec/**/*.coffee'])
.pipe($.istanbul.writeReports({
dir: './coverage',
reportOpts: {
dir: './coverage'
},
reporters: ['html']
})).pipe($.mocha({
reporter: 'spec',
require: 'coffee-script/register'
}));
});
});
There is a ticket related to this which I found referenced in the gulp-istanbul code just before some voodoo magic to make things work: https://github.com/gotwarlost/istanbul/issues/112
有一张与此相关的票,我在 gulp-istanbul 代码中找到了在一些巫毒魔法使事情起作用之前引用的票:https: //github.com/gotwarlost/istanbul/issues/112
回答by Joel Grenon
Istanbul hook is executed when the file is required. So, you need to require all files in order for them to be included in the final coverage report. You can achieve this by injecting a tap inside your gulp task and call require on all selected files:
当需要文件时执行伊斯坦布尔钩子。因此,您需要要求所有文件才能将它们包含在最终覆盖率报告中。你可以通过在你的 gulp 任务中注入一个 tap 并在所有选定的文件上调用 require 来实现这一点:
gulp.task( 'test', function () {
gulp.src( [ my source glob ] )
.pipe( istanbul() )
.pipe(tap(function(f) {
// Make sure all files are loaded to get accurate coverage data
require(f.path);
}))
.on( 'end', function () {
gulp.src( [ my test spec glob ] )
.pipe( mocha( {
reporter: 'spec'
} ) )
.pipe( istanbul.writeReports(
[ output location ]
) );
} );
} );
回答by OmarCastro
to show the coverage in all files you must do 2 things:
要显示所有文件的覆盖率,您必须做两件事:
All files in the project should be included in the
[my source glob]
.All files must be required, in other words, the
require()
must be called on all files in the project, because istanbul probably changes the defaultrequire()
in order to calculate the coverage in a file.
项目中的所有文件都应包含在
[my source glob]
.必须要求所有文件,换句话说,
require()
必须在项目中的所有文件上调用 ,因为伊斯坦布尔可能会更改默认值require()
以计算文件中的覆盖率。
You can write something like this at the start of the test file:
您可以在测试文件的开头编写如下内容:
for([all .js files in project]){
require([file])
}
You can use require-dirfor that.
您可以为此使用require-dir。
And use this instead:
并改用这个:
var requireDir = require('require-dir');
var dir = requireDir('./projectFolder', {recurse: true});