forked from mochajs/mocha
/
stats-collector.js
70 lines (58 loc) · 1.49 KB
/
stats-collector.js
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
'use strict';
/**
* Test statistics collector.
*
* @typedef {Object} StatsCollector
* @property {number} suites - integer count of suites run.
* @property {number} tests - integer count of tests run.
* @property {number} passes - integer count of passing tests.
* @property {number} pending - integer count of pending tests.
* @property {number} failures - integer count of failed tests.
* @property {Date} start - time when testing began.
* @property {Date} end - time when testing concluded.
* @property {number} duration - number of msecs that testing took.
*/
/**
* Provides stats such as test duration,
* number of tests passed / failed etc.
*
* @public
* @memberof Mocha
* @param {Runner} runner
*/
function createStatsCollector(runner) {
var stats = {
suites: 0,
tests: 0,
passes: 0,
pending: 0,
failures: 0
};
if (!runner) {
throw new TypeError('Missing runner argument');
}
runner.stats = stats;
runner.once('start', function() {
stats.start = new Date();
});
runner.on('suite', function(suite) {
suite.root || stats.suites++;
});
runner.on('pass', function() {
stats.passes++;
});
runner.on('fail', function() {
stats.failures++;
});
runner.on('pending', function() {
stats.pending++;
});
runner.on('test end', function() {
stats.tests++;
});
runner.once('end', function() {
stats.end = new Date();
stats.duration = stats.end - stats.start;
});
}
module.exports = createStatsCollector;