Home:ALL Converter>Full Gulp Istanbul Coverage Report

Full Gulp Istanbul Coverage Report

Ask Time:2014-03-28T09:34:46         Author:Scott

Json Formatter

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.

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.

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 ]
                ) );
        } );
} );

Author:Scott,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/22702578/full-gulp-istanbul-coverage-report
Romain Braun :

It actually is much more simple now and you just have to add includeUntested to your istanbul() call.\n\ngulp.task('test', function () {\n return gulp.src('./assets/**/js/*.js')\n // Right there\n .pipe(istanbul({includeUntested: true}))\n .on('finish', function () {\n gulp.src('./assets/js/test/test.js')\n .pipe(mocha({reporter: 'spec'}))\n .pipe(istanbul.writeReports({\n dir: './assets/unit-test-coverage',\n reporters: [ 'lcov' ],\n reportOpts: { dir: './assets/unit-test-coverage'}\n }));\n });\n });\n\n\nSource : https://github.com/SBoudrias/gulp-istanbul#includeuntested",
2014-10-17T06:01:18
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: \n\ngulp.task( 'test', function () {\n gulp.src( [ my source glob ] )\n .pipe( istanbul() )\n .pipe(tap(function(f) {\n // Make sure all files are loaded to get accurate coverage data\n require(f.path);\n }))\n .on( 'end', function () {\n gulp.src( [ my test spec glob ] )\n .pipe( mocha( {\n reporter: 'spec'\n } ) )\n .pipe( istanbul.writeReports(\n [ output location ]\n ) );\n } );\n} );\n",
2014-07-16T15:03:03
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.\n\ngulp.task('test', ['build'], function(done) {\n gulp.src('lib/**/*.js')\n .pipe($.istanbul({ includeUntested: true }))\n .on('end', function() {\n gulp.src(['test/helpers/**/*.coffee', 'test/spec/**/*.coffee'])\n .pipe($.istanbul.writeReports({\n dir: './coverage',\n reportOpts: {\n dir: './coverage'\n },\n reporters: ['html']\n })).pipe($.mocha({\n reporter: 'spec',\n require: 'coffee-script/register'\n }));\n });\n});\n\n\nThere 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",
2014-10-29T01:49:22
yy