Skip to content

Commit 4ab1958

Browse files
committed
some style fixes, avoid logging coverage if empty, inc version
1 parent fcf6668 commit 4ab1958

5 files changed

Lines changed: 67 additions & 35 deletions

File tree

lib/coverage.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,72 @@ var path = require('path'),
44

55
var istanbul, collector;
66

7-
try { istanbul = require('istanbul'); }
8-
catch (e) {}
7+
try {
8+
istanbul = require('istanbul');
9+
} catch (e) {}
910

1011
exports.setup = function() {
1112
collector = new istanbul.Collector();
1213
};
1314

1415
exports.add = function(coverage) {
15-
collector && coverage && collector.add(coverage);
16+
if (collector && coverage) collector.add(coverage);
1617
};
1718

1819
exports.get = function() {
20+
var summaries
1921
if (collector) {
20-
var summaries = [];
21-
collector.files().forEach(function (file) {
22+
summaries = [];
23+
collector.files().forEach(function(file) {
2224
summaries.push(istanbul.utils.summarizeFileCoverage(collector.fileCoverageFor(file)));
2325
});
2426
return istanbul.utils.mergeSummaryObjects.apply(null, summaries);
2527
}
2628
};
2729

2830
exports.report = function() {
31+
var opts, Report, reports;
32+
2933
if (collector) {
30-
var opts = { dir: path.resolve('coverage') },
31-
Report = istanbul.Report,
32-
reports = [ Report.create('lcov', opts), Report.create('json', opts) ];
33-
reports.forEach(function (rep) {
34+
opts = {dir: path.resolve('coverage')};
35+
Report = istanbul.Report;
36+
reports = [Report.create('lcov', opts), Report.create('json', opts)];
37+
reports.forEach(function(rep) {
3438
rep.writeReport(collector, true);
3539
});
3640
}
3741
};
3842

3943
function resolvePaths(files) {
40-
if (Array.isArray(files)) return files.map(function (file) {
41-
return file.path;
42-
});
44+
if (Array.isArray(files)) {
45+
return files.map(function (file) {
46+
return file.path;
47+
});
48+
}
4349
return [files.path];
4450
}
4551

46-
exports.instrument = function(options, cb) {
52+
exports.instrument = function(options, callback) {
4753
istanbul.matcherFor({
4854
includes: resolvePaths(options.code),
4955
excludes: resolvePaths(options.tests)
5056
}, function (err, matcher) {
51-
if (err) { throw err; }
52-
var instrumenter = new istanbul.Instrumenter();
57+
var instrumenter
58+
59+
if (err) {
60+
return callback(err)
61+
}
62+
63+
instrumenter = new istanbul.Instrumenter();
5364
istanbul.hook.hookRequire(matcher, instrumenter.instrumentSync.bind(instrumenter));
54-
cb();
65+
callback();
5566
});
5667
};
5768

5869
if (!istanbul) {
5970
_.each(exports, function(fn, name) {
6071
exports[name] = function() {
61-
util.error('Module "istanbul" is not installed.'.red);
72+
util.error('\nModule "istanbul" is not installed.'.red);
6273
process.exit(1);
6374
};
6475
});

lib/log.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ data = {
1111
coverages: []
1212
};
1313

14-
exports.add = function(t, d) {
15-
if (d) {
16-
data[t].push(d);
14+
/**
15+
* Add data to the log report.
16+
*
17+
* @param {String} type
18+
* @param {Object} obj
19+
* @return {Array}
20+
*/
21+
exports.add = function(type, obj) {
22+
if (obj) {
23+
data[type].push(obj);
1724
}
18-
return data[t];
25+
return data[type];
1926
};
2027

2128
/**
@@ -216,7 +223,11 @@ function getMet(metric) {
216223
}
217224

218225
print.coverage = function() {
219-
var table = new Table({
226+
var table;
227+
228+
if (!data.coverages.length) return
229+
230+
table = new Table({
220231
head: ['File', 'Statements', 'Branches', 'Functions', 'Lines'],
221232
colWidths: [fileColWidth + 2, 14, 14, 14, 14]
222233
});
@@ -229,14 +240,23 @@ print.coverage = function() {
229240
};
230241

231242
print.globalCoverage = function() {
232-
var data = exports.stats().coverage;
243+
var coverage, table;
233244

234-
var table = new Table({
245+
if (!data.coverages.length) return
246+
247+
coverage = exports.stats().coverage
248+
table = new Table({
235249
head: ['Files', 'Statements', 'Branches', 'Functions', 'Lines'],
236250
colWidths: [8, 14, 14, 14, 14]
237251
});
238252

239-
table.push([data.files, getMet(data.statements), getMet(data.branches), getMet(data.functions), getMet(data.lines)]);
253+
table.push([
254+
coverage.files,
255+
getMet(coverage.statements),
256+
getMet(coverage.branches),
257+
getMet(coverage.functions),
258+
getMet(coverage.lines)
259+
]);
240260

241261
log('\nGlobal coverage:\n' + table.toString());
242262
};

lib/testrunner.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ exports.run = function(files, callback) {
150150
files = [files];
151151
}
152152

153-
options.coverage && coverage.setup();
153+
if (options.coverage) coverage.setup();
154154

155155
files.forEach(function(file) {
156156
var opts = _.extend({}, options, file);
@@ -160,7 +160,7 @@ exports.run = function(files, callback) {
160160
opts.code = absPath(opts.code);
161161
opts.tests = absPaths(opts.tests);
162162

163-
function done(err, stat) {
163+
runOne(opts, function(err, stat) {
164164
if (err) {
165165
return callback(err, log.stats());
166166
}
@@ -173,12 +173,12 @@ exports.run = function(files, callback) {
173173
log.print[name]();
174174
}
175175
});
176-
// write coverage report(s)
177-
coverage.report();
176+
177+
// Write coverage report.
178+
if (opts.coverage) coverage.report();
178179
callback(null, log.stats());
179180
}
180-
}
181-
runOne(opts, done);
181+
});
182182
});
183183
};
184184

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "qunit",
33
"description": "QUnit testing framework for nodejs",
4-
"version": "0.5.18",
4+
"version": "0.6.0",
55
"author": "Oleg Slobodskoi <oleg008@gmail.com>",
66
"contributors": [
77
{"name": "Jonathan Buchanan"},
8-
{"name": "Ashar Voultoiz"}
8+
{"name": "Ashar Voultoiz"},
9+
{"name": "Drew Fyock"}
910
],
1011
"repository": {
1112
"type": "git",

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ http://github.com/jquery/qunit
2727
#### The only exception
2828

2929
// Separate tests into modules.
30-
// Use `QUnit` namespace, because `module` is reserved
30+
// Use `QUnit` namespace, because `module` is reserved for node.
3131
QUnit.module(name, lifecycle)
3232

3333
### Usage
@@ -239,4 +239,4 @@ Some tests examples
239239

240240
### Coverage
241241

242-
Code coverage via Istanbul. To utilize, install `istanbul` and set `coverage: true`. Coverage calculations based on code and tests passed to `node-qunit`.
242+
Code coverage via Istanbul. To utilize, install `istanbul` and set option `coverage: true` or pass `--cov` parameter in the shell. Coverage calculations based on code and tests passed to `node-qunit`.

0 commit comments

Comments
 (0)