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'], function() { gulp.src('test/**/*.js') .pipe(mocha()) .pipe(istanbul.writeReports()) .pipe(istanbul.enforceThresholds({ thresholds: { global:90 } })); });
Now, when we run...