1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/**
* Module dependencies.
*/
var Base = require('./base');
/**
* Expose `JSONCov`.
*/
exports = module.exports = JSONCov;
/**
* Initialize a new `JsCoverage` reporter.
*
* @api public
* @param {Runner} runner
* @param {boolean} output
*/
function JSONCov(runner, output) {
Base.call(this, runner);
output = arguments.length === 1 || output;
var self = this;
var tests = [];
var failures = [];
var passes = [];
runner.on('test end', function(test) {
tests.push(test);
});
runner.on('pass', function(test) {
passes.push(test);
});
runner.on('fail', function(test) {
failures.push(test);
});
runner.on('end', function() {
var cov = global._$jscoverage || {};
var result = self.cov = map(cov);
result.stats = self.stats;
result.tests = tests.map(clean);
result.failures = failures.map(clean);
result.passes = passes.map(clean);
if (!output) {
return;
}
process.stdout.write(JSON.stringify(result, null, 2));
});
}
/**
* Map jscoverage data to a JSON structure
* suitable for reporting.
*
* @api private
* @param {Object} cov
* @return {Object}
*/
function map(cov) {
var ret = {
instrumentation: 'node-jscoverage',
sloc: 0,
hits: 0,
misses: 0,
coverage: 0,
files: []
};
for (var filename in cov) {
if (Object.prototype.hasOwnProperty.call(cov, filename)) {
var data = coverage(filename, cov[filename]);
ret.files.push(data);
ret.hits += data.hits;
ret.misses += data.misses;
ret.sloc += data.sloc;
}
}
ret.files.sort(function(a, b) {
return a.filename.localeCompare(b.filename);
});
if (ret.sloc > 0) {
ret.coverage = (ret.hits / ret.sloc) * 100;
}
return ret;
}
/**
* Map jscoverage data for a single source file
* to a JSON structure suitable for reporting.
*
* @api private
* @param {string} filename name of the source file
* @param {Object} data jscoverage coverage data
* @return {Object}
*/
function coverage(filename, data) {
var ret = {
filename: filename,
coverage: 0,
hits: 0,
misses: 0,
sloc: 0,
source: {}
};
data.source.forEach(function(line, num) {
num++;
if (data[num] === 0) {
ret.misses++;
ret.sloc++;
} else if (data[num] !== undefined) {
ret.hits++;
ret.sloc++;
}
ret.source[num] = {
source: line,
coverage: data[num] === undefined ? '' : data[num]
};
});
ret.coverage = ret.hits / ret.sloc * 100;
return ret;
}
/**
* Return a plain-object representation of `test`
* free of cyclic properties etc.
*
* @api private
* @param {Object} test
* @return {Object}
*/
function clean(test) {
return {
duration: test.duration,
currentRetry: test.currentRetry(),
fullTitle: test.fullTitle(),
title: test.title
};
}
|