Skip to content

Commit

Permalink
Added --json [file] flag to allow for easier automated reporting of c…
Browse files Browse the repository at this point in the history
…overage

Signed-off-by: Tj Holowaychuk <tj@vision-media.ca>
  • Loading branch information
Joshua Cohen authored and tj committed Jun 13, 2011
1 parent ce122fb commit 6e8af54
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 88 deletions.
60 changes: 53 additions & 7 deletions bin/expresso
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ try {
* Expresso version.
*/

var version = '0.7.7';
var version = '0.7.8';

/**
* Failure count.
Expand Down Expand Up @@ -90,6 +90,12 @@ var timeout = 2000;

var quiet = false;

/**
* JSON code coverage report
*/
var jsonCoverage = false;
var jsonFile;

/**
* Usage documentation.
*/
Expand All @@ -100,6 +106,7 @@ var usage = ''
+ '\n[bold]{Options}:'
+ '\n -g, --growl Enable growl notifications'
+ '\n -c, --coverage Generate and report test coverage'
+ '\n -j, --json Used in conjunction with --coverage, emit the coverage report as JSON instead of a table'
+ '\n -q, --quiet Suppress coverage report if 100%'
+ '\n -t, --timeout MS Timeout in milliseconds, defaults to 2000'
+ '\n -r, --require PATH Require the given module path'
Expand Down Expand Up @@ -187,8 +194,8 @@ while (args.length) {
break;
case '-b':
case '--boring':
boring = true;
break;
boring = true;
break;
case '-g':
case '--growl':
growl = true;
Expand All @@ -197,6 +204,15 @@ while (args.length) {
case '--serial':
serial = true;
break;
case '-j':
case '--json':
jsonCoverage = true;
if (arg = args.shift()) {
jsonFile = path.normalize(arg);
} else {
throw new Error('--json requires file to write to');
}
break;
default:
if (file_matcher.test(arg)) {
files.push(arg);
Expand Down Expand Up @@ -523,12 +539,12 @@ function rpad(str, width) {
}

/**
* Report test coverage.
* Report test coverage in tabular format
*
* @param {Object} cov
*/

function reportCoverage(cov) {
function reportCoverageTable(cov) {
// Stats
print('\n [bold]{Test Coverage}\n');
var sep = ' +------------------------------------------+----------+------+------+--------+',
Expand Down Expand Up @@ -568,6 +584,36 @@ function reportCoverage(cov) {
}
}

/**
* Report test coverage in raw json format
*
* @param {Object} cov
*/

function reportCoverageJson(cov) {
var report = {
"coverage" : cov.coverage.toFixed(2),
"LOC" : cov.LOC,
"SLOC" : cov.SLOC,
"totalMisses" : cov.totalMisses,
"files" : {}
};

for (var name in cov) {
var file = cov[name];
if (Array.isArray(file)) {
report.files[name] = {
"coverage" : file.coverage.toFixed(2),
"LOC" : file.LOC,
"SLOC" : file.SLOC,
"totalMisses" : file.totalMisses
};
}
}

fs.writeFileSync(jsonFile, JSON.stringify(report), "utf8");
}

/**
* Populate code coverage data.
*
Expand Down Expand Up @@ -640,7 +686,7 @@ function hasFullCoverage(cov) {
var file = cov[name];
if (file instanceof Array) {
if (file.coverage !== 100) {
return false;
return false;
}
}
}
Expand Down Expand Up @@ -832,7 +878,7 @@ function report() {
if (typeof _$jscoverage === 'object') {
populateCoverage(_$jscoverage);
if (!hasFullCoverage(_$jscoverage) || !quiet) {
reportCoverage(_$jscoverage);
(jsonCoverage ? reportCoverageJson(_$jscoverage) : reportCoverageTable(_$jscoverage));
}
}
}
Expand Down
117 changes: 74 additions & 43 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
<html>
<head>
<title>Expresso - TDD Framework For Node</title>
<style>
body {
font: 13px/1.4 "Helvetica", "Lucida Grande", Arial, sans-serif;
text-align: center;
}
#ribbon {
position: absolute;
top: 0;
right: 0;
z-index: 10;
}
#wrapper {
margin: 0 auto;
padding: 50px 80px;
width: 700px;
text-align: left;
}
h1, h2, h3 {
margin: 25px 0 15px 0;
}
h1 {
font-size: 35px;
}
pre {
margin: 0 5px;
padding: 15px;
border: 1px solid #eee;
}
a {
color: #00aaff;
}
</style>
</head>
<body>
<a href="http://github.com/visionmedia/expresso">
<img alt="Fork me on GitHub" id="ribbon" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" />
</a>
<div id="wrapper">
<h1>Expresso</h1>
<head>
<title>Expresso - TDD Framework For Node</title>
<style>
body {
font: 13px/1.4 "Helvetica", "Lucida Grande", Arial, sans-serif;
text-align: center;
}
#ribbon {
position: absolute;
top: 0;
right: 0;
z-index: 10;
}
#wrapper {
margin: 0 auto;
padding: 50px 80px;
width: 700px;
text-align: left;
}
h1, h2, h3 {
margin: 25px 0 15px 0;
}
h1 {
font-size: 35px;
}
pre {
margin: 0 5px;
padding: 15px;
border: 1px solid #eee;
}
a {
color: #00aaff;
}
</style>
</head>
<body>
<a href="http://github.com/visionmedia/expresso">
<img alt="Fork me on GitHub" id="ribbon" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" />
</a>
<div id="wrapper">
<h1>Expresso</h1>
<div class='mp'>
<h2 id="NAME">NAME</h2>
<p class="man-name">
Expand Down Expand Up @@ -361,6 +361,37 @@ <h2 id="expresso-1-">expresso(1)</h2>
<p>Currently coverage is bound to the <em>lib</em> directory, however in the
future <code>--cov</code> will most likely accept a path.</p>

<p>If you would like code coverage reports suitable for automated parsing, pass the <code>--json [output file]</code> option:</p>

<pre><code>$ expresso -I lib test/*
$ expresso -I lib --cov --json coverage.json test/*
</code></pre>

<p>You should then see the json coverage details in the file you specified:</p>

<pre><code>
{
"LOC": 20,
"SLOC": 7,
"coverage": "71.43",
"files": {
"bar.js": {
"LOC": 4,
"SLOC": 2,
"coverage": "100.00",
"totalMisses": 0
},
"foo.js": {
"LOC": 16,
"SLOC": 5,
"coverage": "60.00",
"totalMisses": 2
}
},
"totalMisses": 2
}
</code></pre>

<h2 id="Async-Exports">Async Exports</h2>

<p>Sometimes it is useful to postpone running of tests until a callback or event has fired, currently the <em>exports.foo = function(){};</em> syntax is supported for this:</p>
Expand All @@ -373,6 +404,6 @@ <h2 id="Async-Exports">Async Exports</h2>
</code></pre>

</div>
</div>
</body>
</div>
</body>
</html>
Loading

0 comments on commit 6e8af54

Please sign in to comment.