Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
thostetler committed Sep 7, 2017
1 parent 713c61c commit ea4587d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
20 changes: 12 additions & 8 deletions grunt/config/coverage-hook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

// phantomjs's version of `fs`
var fs = require('fs');
var COVERAGE_FILE = 'test/coverage/coverage.json';

Expand All @@ -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 = {
Expand Down
35 changes: 32 additions & 3 deletions grunt/mocha-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}, {});

Expand All @@ -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({
Expand Down Expand Up @@ -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();
});
});
Expand Down

0 comments on commit ea4587d

Please sign in to comment.