gulp-babel-istanbul

raw JSON →
1.6.0 verified Sat Apr 25 auth: no javascript maintenance

A gulp plugin that instruments source files with Istanbul code coverage after Babel transpilation, enabling ES2015+ coverage reporting. Version 1.6.0 (latest) uses babel-istanbul under the hood. It integrates with any Node.js test framework via gulp, providing hooks for require and reports. Compared to alternatives like gulp-istanbul, it natively handles Babel-transpiled code. Browser testing is untested. Requires gulp, babel, and Istanbul. Slow release cadence; last update 2016. Consider seamless builds with webpack or karma for browser coverage.

error Error: Cannot find module 'babel/register'
cause Plugin expects babel-istanbul but babel-core is not installed or outdated
fix
npm install babel-core@^6 --save-dev
error TypeError: istanbul.hookRequire is not a function
cause hookRequire is not called on the default export directly
fix
Use require('gulp-babel-istanbul').hookRequire or import { hookRequire } from 'gulp-babel-istanbul'
error ENOENT: no such file or directory, open 'coverage/lcov.info'
cause writeReports expects a coverage variable that may not exist
fix
Ensure istanbul() and hookRequire() are applied before running tests
error The coverage report is empty
cause includeUntested option false by default and require hook not applied
fix
Set { includeUntested: true } and ensure hookRequire() is piped
gotcha hookRequire must be used in the same stream after istanbul()
fix Call .pipe(istanbul.hookRequire()) before the finish event.
gotcha Browser testing is untested and may not work
fix Use Karma with karma-coverage instead for browser coverage.
deprecated babel-core is replaced by @babel/core
fix Use gulp-babel with @babel/core and @babel/preset-env; this plugin uses babel-istanbul which may not be updated.
gotcha require hook does not work with ESM modules
fix Use CommonJS modules or transpile to CJS before testing.
npm install gulp-babel-istanbul
yarn add gulp-babel-istanbul
pnpm add gulp-babel-istanbul

Shows complete gulp task for instrumenting ES2015 source, running mocha tests, and generating coverage reports with threshold enforcement.

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

gulp.task('coverage', function (cb) {
  gulp.src('src/**/*.js')
    .pipe(istanbul())
    .pipe(istanbul.hookRequire())
    .on('finish', function () {
      gulp.src('test/**/*.js')
        .pipe(babel())
        .pipe(mocha())
        .pipe(istanbul.writeReports())
        .pipe(istanbul.enforceThresholds({ thresholds: { global: 90 } }))
        .on('end', cb);
    });
});