Gathering code coverage statistics

Although we have some tests for our application, they are certainly not yet comprehensive. It would be useful to be able to see what parts of our code are covered by tests. For this, we'll use Istanbul, a JavaScript code coverage tool. First, install the gulp-instanbul plugin:

> npm install gulp-istanbul --save-dev

Now we need to add a Gulp task to instrument our production code for coverage:

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

...

gulp.task('instrument', function() {
    return gulp.src('src/**/*.js')
        .pipe(istanbul())
        .pipe(istanbul.hookRequire())
});

Finally, we need to update our test task to output a coverage report and fail the build if we are below our threshold:

gulp.task('test', ['lint-test', 'instrument'], ...

Get Learning Node.js for .NET Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.