forked from tnicola/cypress-parallel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-stream.reporter.js
87 lines (84 loc) · 2.18 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
/**
* @module JSONStreamCustom
*/
/**
* Module dependencies.
*/
var Base = require('mocha/lib/reporters/base');
var constants = require('mocha/lib/runner').constants;
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
var EVENT_SUITE_END = constants.EVENT_SUITE_END;
/**
* 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;
runner.once(EVENT_RUN_BEGIN, function() {
writeEvent(['start', { total: total }]);
});
runner.on(EVENT_TEST_PASS, function(test) {
writeEvent(['pass', clean(test)]);
});
runner.on(EVENT_TEST_FAIL, function(test, err) {
test = clean(test);
test.err = err.message;
test.stack = err.stack || null;
writeEvent(['fail', test]);
});
runner.on(EVENT_SUITE_END, function(suite) {
writeEvent(['suiteEnd', cleanSuite(suite, self.stats)]);
});
}
/**
* Mocha event to be written to the output stream.
* @typedef {Array} JSONStream~MochaEvent
*/
/**
* Writes Mocha event to reporter output stream.
*
* @private
* @param {JSONStreamCustom~MochaEvent} event - Mocha event to be output.
*/
function writeEvent(event) {
process.stdout.write(JSON.stringify(event) + '\n');
}
/**
* Returns an object literal representation of `test`
* free of cyclic properties, etc.
*
* @private
* @param {Test} test - Instance used as data source.
* @return {Object} object containing pared-down test instance data
*/
function clean(test) {
return {
title: test.title,
fullTitle: test.fullTitle(),
file: test.file,
duration: test.duration,
currentRetry: test.currentRetry()
};
}
function cleanSuite(suite, stats) {
return {
...stats,
title: suite.file
};
}
JSONStreamCustom.description = 'JSONStream customized reporter';