Debian Codegraph @

lib/nodejs/reporters/parallel-buffered.js

raw
1 /**
2 * "Buffered" reporter used internally by a worker process when running in parallel mode.
3 * @module nodejs/reporters/parallel-buffered
4 * @public
5 */
6
7 "use strict";
8
9 /**
10 * @typedef {import('../../types.d.ts').BufferedEvent} BufferedEvent
11 * @typedef {import('../../runner.js')} Runner
12 */
13
14 /**
15 * Module dependencies.
16 */
17
18 const {
19 EVENT_SUITE_BEGIN,
20 EVENT_SUITE_END,
21 EVENT_TEST_FAIL,
22 EVENT_TEST_PASS,
23 EVENT_TEST_PENDING,
24 EVENT_TEST_BEGIN,
25 EVENT_TEST_END,
26 EVENT_TEST_RETRY,
27 EVENT_DELAY_BEGIN,
28 EVENT_DELAY_END,
29 EVENT_HOOK_BEGIN,
30 EVENT_HOOK_END,
31 EVENT_RUN_END,
32 } = require("../../runner").constants;
33 const {
34 SerializableEvent,
35 SerializableWorkerResult,
36 } = require("../serializer");
37 const debug = require("debug")("mocha:reporters:buffered");
38 const Base = require("../../reporters/base");
39
40 /**
41 * List of events to listen to; these will be buffered and sent
42 * when `Mocha#run` is complete (via {@link ParallelBuffered#done}).
43 */
44 const EVENT_NAMES = [
45 EVENT_SUITE_BEGIN,
46 EVENT_SUITE_END,
47 EVENT_TEST_BEGIN,
48 EVENT_TEST_PENDING,
49 EVENT_TEST_FAIL,
50 EVENT_TEST_PASS,
51 EVENT_TEST_RETRY,
52 EVENT_TEST_END,
53 EVENT_HOOK_BEGIN,
54 EVENT_HOOK_END,
55 ];
56
57 /**
58 * Like {@link EVENT_NAMES}, except we expect these events to only be emitted
59 * by the `Runner` once.
60 */
61 const ONCE_EVENT_NAMES = [EVENT_DELAY_BEGIN, EVENT_DELAY_END];
62
63 /**
64 * The `ParallelBuffered` reporter is used by each worker process in "parallel"
65 * mode, by default. Instead of reporting to `STDOUT`, etc., it retains a
66 * list of events it receives and hands these off to the callback passed into
67 * {@link Mocha#run}. That callback will then return the data to the main
68 * process.
69 * @public
70 */
71 class ParallelBuffered extends Base {
72 /**
73 * Calls {@link ParallelBuffered#createListeners}
74 * @param {Runner} runner
75 */
76 constructor(runner, opts) {
77 super(runner, opts);
78
79 /**
80 * Retained list of events emitted from the {@link Runner} instance.
81 * @type {BufferedEvent[]}
82 * @public
83 */
84 this.events = [];
85
86 /**
87 * Map of `Runner` event names to listeners (for later teardown)
88 * @public
89 * @type {Map<string,EventListener>}
90 */
91 this.listeners = new Map();
92
93 this.createListeners(runner);
94 }
95
96 /**
97 * Returns a new listener which saves event data in memory to
98 * {@link ParallelBuffered#events}. Listeners are indexed by `eventName` and stored
99 * in {@link ParallelBuffered#listeners}. This is a defensive measure, so that we
100 * don't a) leak memory or b) remove _other_ listeners that may not be
101 * associated with this reporter.
102 *
103 * Subclasses could override this behavior.
104 *
105 * @public
106 * @param {string} eventName - Name of event to create listener for
107 * @returns {EventListener}
108 */
109 createListener(eventName) {
110 const listener = (runnable, err) => {
111 this.events.push(SerializableEvent.create(eventName, runnable, err));
112 };
113 return this.listeners.set(eventName, listener).get(eventName);
114 }
115
116 /**
117 * Creates event listeners (using {@link ParallelBuffered#createListener}) for each
118 * reporter-relevant event emitted by a {@link Runner}. This array is drained when
119 * {@link ParallelBuffered#done} is called by {@link Runner#run}.
120 *
121 * Subclasses could override this behavior.
122 * @public
123 * @param {Runner} runner - Runner instance
124 * @returns {ParallelBuffered}
125 * @chainable
126 */
127 createListeners(runner) {
128 EVENT_NAMES.forEach((evt) => {
129 runner.on(evt, this.createListener(evt));
130 });
131 ONCE_EVENT_NAMES.forEach((evt) => {
132 runner.once(evt, this.createListener(evt));
133 });
134
135 runner.once(EVENT_RUN_END, () => {
136 debug("received EVENT_RUN_END");
137 this.listeners.forEach((listener, evt) => {
138 runner.removeListener(evt, listener);
139 this.listeners.delete(evt);
140 });
141 });
142
143 return this;
144 }
145
146 /**
147 * Calls the {@link Mocha#run} callback (`callback`) with the test failure
148 * count and the array of {@link BufferedEvent} objects. Resets the array.
149 *
150 * This is called directly by `Runner#run` and should not be called by any other consumer.
151 *
152 * Subclasses could override this.
153 *
154 * @param {number} failures - Number of failed tests
155 * @param {Function} callback - The callback passed to {@link Mocha#run}.
156 * @public
157 */
158 done(failures, callback) {
159 callback(SerializableWorkerResult.create(this.events, failures));
160 this.events = []; // defensive
161 }
162 }
163
164 module.exports = ParallelBuffered;