Last active
August 29, 2015 13:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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