Skip to content

Instantly share code, notes, and snippets.

@jonbretman
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbretman/8947386 to your computer and use it in GitHub Desktop.
Save jonbretman/8947386 to your computer and use it in GitHub Desktop.
Simple grunt task for instrumenting javascript files and then generating reports from coverage data
grunt.registerMultiTask('coverage', function () {
if (this.target === 'instrument') {
var ignore = this.data.ignore || [];
var Instrumenter = require('istanbul').Instrumenter;
var instrumenter = new Instrumenter();
this.files.forEach(function (file) {
var src = file.src[0];
var instrumented = grunt.file.read(src);
// only instrument this file if it is not in ignored list
if (!grunt.file.isMatch(ignore, src)) {
instrumented = instrumenter.instrumentSync(instrumented, src);
}
// write
grunt.file.write(file.dest, instrumented);
});
return;
}
if (this.target === 'report') {
this.requiresConfig('coverage.coverage');
var istanbul = require('istanbul');
var Report = istanbul.Report;
var Collector = istanbul.Collector;
var reporters = this.data.reports;
var dest = this.data.dest;
var collector = new Collector();
collector.add(grunt.config('coverage.coverage'));
reporters.forEach(function (reporter) {
Report.create(reporter, {
dir: dest + reporter
}).writeReport(collector, true);
});
return;
}
grunt.warn('Unknown target - valid targets are "instrument" and "report"');
});
module.exports = function (grunt) {
grunt.initConfig({
coverage: {
// set this to the coverage object created from running the instrumented code
coverage: null,
// task for generating instrumented code
instrument: {
// files to NOT instrument eg. libs
ignore: [
'src/js/libs/**/*'
],
// file to instrument
files: [
{
src: '**/*.js',
expand: true,
cwd: 'src',
dest: 'instrumented'
}
]
},
// task for generating reports
report: {
reports: ['html', 'text-summary'],
dest: 'tests/reports/'
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment