-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
buffered.js
114 lines (106 loc) · 2.96 KB
/
buffered.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
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
'use strict';
/**
* @module Buffered
*/
/**
* Module dependencies.
*/
const {
EVENT_SUITE_BEGIN,
EVENT_SUITE_END,
EVENT_TEST_FAIL,
EVENT_TEST_PASS,
EVENT_TEST_PENDING
} = require('../runner').constants;
/**
* Creates a {@link BufferedEvent} from a {@link Suite}.
* @param {string} evt - Event name
* @param {Suite} suite - Suite object
* @returns {BufferedEvent}
*/
const serializeSuite = (evt, suite) => ({
name: evt,
data: {root: suite.root, title: suite.title}
});
/**
* Creates a {@link BufferedEvent} from a {@link Test}.
* @param {string} evt - Event name
* @param {Test} test - Test object
* @param {any} err - Error, if applicable
*/
const serializeTest = (evt, test, [err]) => {
const obj = {
title: test.title,
duration: test.duration,
err: test.err,
__fullTitle: test.fullTitle(),
__slow: test.slow(),
__titlePath: test.titlePath()
};
if (err) {
obj.err =
test.err && err instanceof Error
? {
multiple: [...(test.err.multiple || []), err]
}
: err;
}
return {
name: evt,
data: obj
};
};
/**
* The `Buffered` reporter is for use by parallel runs. Instead of outputting
* to `STDOUT`, etc., it retains a list of events it receives and hands these
* off to the callback passed into {@link Mocha#run}. That callback will then
* return the data to the main process.
*/
class Buffered {
/**
* Listens for {@link Runner} events and retains them in an `events` instance prop.
* @param {Runner} runner
*/
constructor(runner) {
/**
* Retained list of events emitted from the {@link Runner} instance.
* @type {BufferedEvent[]}
*/
const events = (this.events = []);
runner
.on(EVENT_SUITE_BEGIN, suite => {
events.push(serializeSuite(EVENT_SUITE_BEGIN, suite));
})
.on(EVENT_SUITE_END, suite => {
events.push(serializeSuite(EVENT_SUITE_END, suite));
})
.on(EVENT_TEST_PENDING, test => {
events.push(serializeTest(EVENT_TEST_PENDING, test));
})
.on(EVENT_TEST_FAIL, (test, err) => {
events.push(serializeTest(EVENT_TEST_FAIL, test, err));
})
.on(EVENT_TEST_PASS, test => {
events.push(serializeTest(EVENT_TEST_PASS, test));
});
}
/**
* Calls the {@link Mocha#run} callback (`callback`) with the test failure
* count and the array of {@link BufferedEvent} objects. Resets the array.
* @param {number} failures - Number of failed tests
* @param {Function} callback - The callback passed to {@link Mocha#run}.
*/
done(failures, callback) {
callback(failures, [...this.events]);
this.events = [];
}
}
/**
* Serializable event data from a `Runner`. Keys of the `data` property
* beginning with `__` will be converted into a function which returns the value
* upon deserialization.
* @typedef {Object} BufferedEvent
* @property {string} name - Event name
* @property {object} data - Event parameters
*/
module.exports = Buffered;