forked from tnicola/cypress-parallel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-stream.reporter.js
67 lines (58 loc) · 1.65 KB
/
json-stream.reporter.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
'use strict';
/**
* @module JSONStreamCustom
*/
/**
* Module dependencies.
*/
var Base = require('mocha/lib/reporters/base');
var constants = require('mocha/lib/runner').constants;
var path = require('path');
var fs = require('fs');
const { resultsPath } = require('./shared-config');
const { EVENT_SUITE_END } = constants;
/**
* Expose `JSONStream`.
*/
exports = module.exports = JSONStreamCustom;
/**
* Constructs a new `JSONStreamCustom` reporter instance.
*
* @public
* @class
* @memberof Mocha.reporters
* @extends Mocha.reporters.Base
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
*/
function JSONStreamCustom(runner, options) {
Base.call(this, runner, options);
var self = this;
var total = runner.total;
function cleanStatistics() {
return {
...self.stats,
duration: calculateDuration(self.stats.start, self.stats.end),
file: self.runner.suite.file
};
}
runner.on(EVENT_SUITE_END, function () {
writeFile(cleanStatistics());
});
}
function calculateDuration(start, end) {
end = end || new Date();
const duration = new Date(end).getTime() - new Date(start).getTime();
return duration;
}
function writeFile(statistics) {
// replace forward and backward slash with _ to generate filename
const fileName = statistics.file.replace(/\\|\//g, '_');
if (!fs.existsSync(resultsPath)) {
fs.mkdirSync(resultsPath);
}
const specResultPath = path.join(resultsPath, `${fileName}.json`);
fs.writeFileSync(specResultPath, JSON.stringify(statistics, null, 2));
}
JSONStreamCustom.description =
'Writes statistics per spec file to result files';