Babel ES2015 transpiling and Istanbul unit test coverage plugin for gulp.
Works on top of any Node.js unit test framework.
npm install gulp-babel-istanbul --save-dev
In your gulpfile.js
:
The following example is a sort of "kitchen sink" example of how to use
gulp-babel-istanbul
.
var babel = require('gulp-babel');
var istanbul = require('gulp-babel-istanbul');
var injectModules = require('gulp-inject-modules');
var mocha = require('gulp-mocha');
gulp.task('coverage', function (cb) {
gulp.src('src/**/*.js')
.pipe(istanbul())
.pipe(istanbul.hookRequire()) // or you could use .pipe(injectModules())
.on('finish', function () {
gulp.src('test/**/*.js')
.pipe(babel())
.pipe(injectModules())
.pipe(mocha())
.pipe(babelIstanbul.writeReports())
.on('end', cb);
});
});
For browser testing, you'll need to write the files covered by istanbul in a directory from where you'll serve these files to the browser running the test. You'll also need a way to extract the value of the coverage variable after the test have runned in the browser.
Browser testing is hard. If you're not sure what to do, then I suggest you take a look at Karma test runner - it has built-in coverage using Istanbul.
var istanbul = require('gulp-babel-istanbul');
gulp.task('test', function (cb) {
gulp.src(['lib/**/*.js', 'main.js'])
.pipe(istanbul()) // Covering files
.pipe(gulp.dest('test-tmp/'))
.on('finish', function () {
gulp.src(['test/*.html'])
.pipe(testFramework())
.pipe(istanbul.writeReports()) // Creating the reports after tests ran
.on('end', cb);
});
});
Instrument files passed in the stream.
Type: Object
(optional)
{
coverageVariable: 'someVariable',
...other Instrumeter options...
}
Type: String
(optional)
Default: '$$cov_' + new Date().getTime() + '$$'
The global variable istanbul uses to store coverage
See also:
Type: Boolean
(optional)
Default: false
Flag to include test coverage of files that aren't require
d by any tests
See also:
Type: Instrumenter
(optional)
Default: istanbul.Instrumenter
Custom Instrumenter to be used instead of the default istanbul one.
var isparta = require('isparta');
var istanbul = require('gulp-babel-istanbul');
gulp.src('lib/**.js')
.pipe(istanbul({
instrumenter: isparta.Instrumenter
}));
NOTE: I don't think you need to use isparta since we use babel-istanbul.
See also:
See:
Overwrite require
so it returns the covered files. The method take an optional option object.
Always use this option if you're running tests in Node.js
get coverage summary details
Type: Object
(optional)
{
coverageVariable: 'someVariable'
}
Type: String
(optional)
Default: '$$cov_' + new Date().getTime() + '$$'
The global variable istanbul uses to store coverage
See also:
Type: Object
{
lines: { total: 4, covered: 2, skipped: 0, pct: 50 },
statements: { total: 4, covered: 2, skipped: 0, pct: 50 },
functions: { total: 2, covered: 0, skipped: 0, pct: 0 },
branches: { total: 0, covered: 0, skipped: 0, pct: 100 }
}
See also:
Create the reports on stream end.
Type: Object
(optional)
{
dir: './coverage',
reporters: [ 'lcov', 'json', 'text', 'text-summary', CustomReport ],
reportOpts: { dir: './coverage' },
coverageVariable: 'someVariable'
}
You can pass individual configuration to a reporter.
{
dir: './coverage',
reporters: [ 'lcovonly', 'json', 'text', 'text-summary', CustomReport ],
reportOpts: {
lcov: {dir: 'lcovonly', file: 'lcov.info'}
json: {dir: 'json', file: 'converage.json'}
},
coverageVariable: 'someVariable'
}
Type: String
(optional)
Default: ./coverage
The folder in which the reports are to be outputted.
Type: Array
(optional)
Default: [ 'lcov', 'json', 'text', 'text-summary' ]
The list of available reporters:
clover
cobertura
html
json
lcov
lcovonly
none
teamcity
text
text-summary
You can also specify one or more custom reporter objects as items in the array. These will be automatically registered with istanbul.
See also require('istanbul').Report.getReportList()
Type: String
(optional)
Default: '$$cov_' + new Date().getTime() + '$$'
The global variable istanbul uses to store coverage
See also:
This was disabled in v1.0.0 because it used istanbul-threshold-checker v0.1.0 which used Istanbul v0.3.x. The Handlebars helpers used by this old version of Istanbul's reporters would override the helpers defined by babel-istanbul. Obviously that's not good.
For now, it just acts a passthrough. It has no effect. This full functionality may return someday. PRs welcome.
A plugin error in the stream if the coverage fails
MIT License (c) Simon Boudrias - 2013