diff --git a/grunt/config/coverage-hook.js b/grunt/config/coverage-hook.js index 6fbafc6cd..e8ca16e70 100644 --- a/grunt/config/coverage-hook.js +++ b/grunt/config/coverage-hook.js @@ -1,5 +1,6 @@ 'use strict'; +// phantomjs's version of `fs` var fs = require('fs'); var COVERAGE_FILE = 'test/coverage/coverage.json'; @@ -13,15 +14,18 @@ var saveCoverage = function (page) { return; } - var json = JSON.stringify(coverage); - console.log(json); - console.log('attempting to write to file: ', COVERAGE_FILE); - fs.writeFile(COVERAGE_FILE, json, function (err) { - if (err) { - throw err; - } - console.error('Coverage File Written: ' + COVERAGE_FILE); + // fix paths to files + var coverageKeys = Object.keys(coverage); + coverageKeys.forEach(function (key) { + coverage[key].path = 'src/js/' + coverage[key].path; }); + + try { + fs.write(COVERAGE_FILE, JSON.stringify(coverage), 'w'); + } catch (e) { + console.log('Unable to write to file...'); + console.error(e); + } }; module.exports = { diff --git a/grunt/mocha-runner.js b/grunt/mocha-runner.js index c7172219f..4186c9bc8 100644 --- a/grunt/mocha-runner.js +++ b/grunt/mocha-runner.js @@ -10,8 +10,11 @@ module.exports = function (grunt) { var async = require('async'); var istanbul = require('istanbul'); var spawn = require('child_process').spawn; - var COVERAGE_PREFIX = 'test/coverage/instrument/'; + var COVERAGE_HTML_OUTPUT_DIR = 'test/coverage/report'; + var COVERAGE_JSON_OUTPUT_FILE = 'test/coverage/report.json'; + var INSTRUMENT_PREFIX = 'test/coverage/instrument/'; var COVERAGE_FILE_NAME = 'test/coverage/mapping.js'; + var COVERAGE_COLLECTION_FILE = 'test/coverage/coverage.json'; /** * Gather all the src paths and prefix them with the coverage path @@ -23,7 +26,7 @@ module.exports = function (grunt) { '!src/js/**/*.jsx.js' // exclude es6 files ]).reduce(function (res, p) { p = p.replace('src/', '').replace(/\.js$/, ''); - res[p] = p.replace('js/', COVERAGE_PREFIX); + res[p] = p.replace('js/', INSTRUMENT_PREFIX); return res; }, {}); @@ -34,7 +37,6 @@ module.exports = function (grunt) { grunt.registerMultiTask('mocha-runner', 'find and run tests', function () { var done = this.async(); - var collector = new istanbul.Collector(); // create the options object var options = this.options({ @@ -123,11 +125,38 @@ module.exports = function (grunt) { // spawn a new test for each url, output total failures async.eachSeries(options.urls, spawnTest, function () { + + // get the coverage object from the collection file generated + var coverageObject = grunt.file.readJSON(COVERAGE_COLLECTION_FILE); + var collector = new istanbul.Collector(); + collector.add(coverageObject); + + // Generate a quick summary to be shown in the output + var finalCoverage = collector.getFinalCoverage(); + var summary = istanbul.utils.summarizeCoverage(finalCoverage); + + // Output the percentages of the coverage + grunt.log.ok('Coverage:'); + grunt.log.ok('- Lines: ' + summary.lines.pct + '%'); + grunt.log.ok('- Statements: ' + summary.statements.pct + '%'); + grunt.log.ok('- Functions: ' + summary.functions.pct + '%'); + grunt.log.ok('- Branches: ' + summary.branches.pct + '%'); + + // write reports + istanbul.Report.create('html', { + dir: COVERAGE_HTML_OUTPUT_DIR + }).writeReport(collector, true); + + istanbul.Report.create('json', { + dir: COVERAGE_JSON_OUTPUT_FILE + }).writeReport(collector, true); + if (errors > 0) { grunt.fail.warn(errors + ' tests failed :(\n'); } else if (errors === 0) { grunt.log.writeln(errors + ' tests failed :)\n'); } + done(); }); });