March 25, 2018

How to config gulp to run unit test cases using Istanbul with code coverage

Add below configuration in gulpfile.js for running Istanbul with mocha unit test cases through gulp.


const gulp = require('gulp');
const istanbul = require('gulp-istanbul');
const mocha = require('gulp-mocha');

gulp.task('test', () => {    
 gulp.src(['src/**/*.js'])
    .pipe(istanbul())
    .pipe(istanbul.hookRequire())
    .on('finish', () => {
      gulp.src(['test/unit/**/*.spec.js'])
        .pipe(mocha())
        .pipe(istanbul.writeReports({
          dir: './test/unit-test-coverage',
          reporters: ['lcov', 'clover', 'text', 'text-summary'],
          reportOpts: {
            dir: './test/unit-test-coverage',
          },
        }));        
    });
 });


For code coverage, we can use istanbul.writeReports method as shown above.

Once executing the command gulp test, it will execute all the test cases along with coverage report generated at given location.

1 comment: