Skip to content

Instantly share code, notes, and snippets.

@PaquitoSoft
Created January 20, 2013 23:00
Show Gist options
  • Save PaquitoSoft/4582423 to your computer and use it in GitHub Desktop.
Save PaquitoSoft/4582423 to your computer and use it in GitHub Desktop.
This is a simplistic reporter Blanket.js (http://migrii.github.com/blanket/) which adds a summary coverage statistic to Jasmine (http://pivotal.github.com/jasmine/) top bar. If you enable branch coverage in blanket, this reporter will highlight the files with warnings and it also change the summary statistic color.
/*
Place this file in your Jasmine tests root folder and tell blanket to use it.
Example:
<script src="lib/blanket_jasmine.js" data-cover-reporter="lib/jasmine.basic.reporter.js"></script>
If you want to catch branch coverage warning, you must enable it:
<script src="lib/blanket_jasmine.js" data-cover-flags="branchTracking" data-cover-reporter="lib/jasmine.basic.reporter.js"></script>
*/
define([], function JasmineBasicReporter() {
/**
Iterate over a list of elements.
@param $elements {NodeList} list of DOM elements
@param fn {Function} callback applied to each element
*/
function iterateEls($elements, fn) {
Array.prototype.forEach.call($elements, fn);
}
/**
Calculates the average covering for all tested files.
Extracts info from default Blanket reporter.
@return averagePencent {Number}
*/
function calculateSummaryPercent() {
var percentTotals = 0,
elements = document.querySelectorAll('#blanket-main .blanket:not(.bl-title) > div:nth-child(2)');
iterateEls(elements, function(filePercentText) {
percentTotals += parseFloat(filePercentText.innerText.split(' ')[0], 10);
});
return percentTotals / elements.length;
}
/**
Creates the new element which represents the report summary.
@param coveragePencent {Number} average files coverage percent
@param branchWarningsCount {Number} number of branch coverage warnings
@return summaryEl {Element}
*/
function buildElement(coveragePercent, branchWarningsCount) {
var el = document.createElement('span');
el.id = "coverageSummary";
el.className = 'bar';
el.style.position = 'absolute';
el.style.top = '0';
el.style.right = '115px';
el.innerHTML = "Coverage: <span style='" + ((branchWarningsCount) ? 'color: yellow' : '') +
"'>" + coveragePercent.toFixed(2) + "%</span>";
console.log(el);
return el;
}
/**
If user enabled branch coverage, I use this function to highlight
the files which have any warning.
Extracts info from default Blanket reporter.
*/
function highlightFilesWithBrachWarnings() {
var $filesReports = document.querySelectorAll('div.blanket:not(.bl-title)'),
$branchWarnings,
counter = 0;
iterateEls($filesReports, function($fileReport) {
$branchWarnings = $fileReport.querySelectorAll('.branchWarning');
if ($branchWarnings.length) {
counter = $branchWarnings.length;
$fileReport.querySelector('.bl-file a').style['background-color'] = 'yellow';
}
});
return counter;
}
/**
Appends coverage summary to Jasmine report.
@param $summary {Element} summary element
*/
function appendSummary($summary) {
$container = document.querySelector('div.alert');
$container.style.position = 'relative';
$container.appendChild($summary);
}
return function(coverageData) {
var averagePercent, $summary, branchWarningsCount;
// Append standard blanket coverage
blanket.defaultReporter(coverageData);
// Calculate average coverage
averagePercent = calculateSummaryPercent();
// If branch coverage was enabled, highlight files with warnings
branchWarningsCount = highlightFilesWithBrachWarnings();
// Create summary coverage element
$summary = buildElement(averagePercent, branchWarningsCount);
// Append new element to Jasmine report
appendSummary($summary);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment