Debian Codegraph @

test/node-unit/reporters/parallel-buffered.spec.js

raw
1 "use strict";
2
3 // this reporter does not actually output anything to the terminal, so we
4 // need to test it differently.
5
6 const {
7 EVENT_SUITE_BEGIN,
8 EVENT_SUITE_END,
9 EVENT_TEST_FAIL,
10 EVENT_TEST_PASS,
11 EVENT_TEST_PENDING,
12 EVENT_TEST_BEGIN,
13 EVENT_TEST_END,
14 EVENT_TEST_RETRY,
15 EVENT_DELAY_BEGIN,
16 EVENT_DELAY_END,
17 EVENT_HOOK_BEGIN,
18 EVENT_HOOK_END,
19 EVENT_RUN_END,
20 } = require("../../../lib/runner").constants;
21 const { EventEmitter } = require("node:events");
22 const sinon = require("sinon");
23 const rewiremock = require("rewiremock/node");
24 const semver = require("semver");
25
26 describe("ParallelBuffered", function () {
27 /** @type {EventEmitter} */
28 let runner;
29 let ParallelBuffered;
30
31 beforeEach(function () {
32 runner = new EventEmitter();
33 ParallelBuffered = rewiremock.proxy(
34 () => require("../../../lib/nodejs/reporters/parallel-buffered"),
35 {
36 "../../../lib/nodejs/serializer": {
37 SerializableEvent: {
38 create: (eventName, runnable, err) => ({
39 eventName,
40 data: runnable,
41 error: err,
42 __type: "MockSerializableEvent",
43 }),
44 },
45 SerializableWorkerResult: {
46 create: (events, failures) => ({
47 events,
48 failures,
49 __type: "MockSerializableWorkerResult",
50 }),
51 },
52 },
53 "../../../lib/reporters/base": class MockBase {},
54 },
55 );
56 });
57
58 afterEach(function () {
59 sinon.restore();
60 });
61
62 describe("constructor", function () {
63 it("should listen for Runner events", function () {
64 // EventEmitter#once calls thru to EventEmitter#on, which
65 // befouls our assertion below.
66 sinon.stub(runner, "once");
67 sinon.stub(runner, "on");
68 // eslint-disable-next-line no-new
69 new ParallelBuffered(runner);
70 expect(runner.on, "to have calls satisfying", [
71 // via Buffered
72 [EVENT_SUITE_BEGIN, expect.it("to be a function")],
73 [EVENT_SUITE_END, expect.it("to be a function")],
74 [EVENT_TEST_BEGIN, expect.it("to be a function")],
75 [EVENT_TEST_PENDING, expect.it("to be a function")],
76 [EVENT_TEST_FAIL, expect.it("to be a function")],
77 [EVENT_TEST_PASS, expect.it("to be a function")],
78 [EVENT_TEST_RETRY, expect.it("to be a function")],
79 [EVENT_TEST_END, expect.it("to be a function")],
80 [EVENT_HOOK_BEGIN, expect.it("to be a function")],
81 [EVENT_HOOK_END, expect.it("to be a function")],
82 ]);
83 });
84
85 it("should listen for Runner events expecting to occur once", function () {
86 sinon.stub(runner, "once");
87 // eslint-disable-next-line no-new
88 new ParallelBuffered(runner);
89 expect(runner.once, "to have calls satisfying", [
90 [EVENT_DELAY_BEGIN, expect.it("to be a function")],
91 [EVENT_DELAY_END, expect.it("to be a function")],
92 [EVENT_RUN_END, expect.it("to be a function")],
93 ]);
94 });
95 });
96
97 describe("event", function () {
98 let reporter;
99
100 beforeEach(function () {
101 reporter = new ParallelBuffered(runner);
102 });
103
104 describe("on EVENT_RUN_END", function () {
105 it("should remove all listeners", function () {
106 runner.emit(EVENT_RUN_END);
107 // Some node versions throw instead of returning `[]` due to a bug
108 // Fix is in Node ^22.14 and ^24.0, but not yet backported to 20 as of writing
109 // Problem was introduced in 20.19.0, not present in 20.18.3
110 // https://github.com/nodejs/node/issues/56263
111 const nodeVersionThrows = semver.satisfies(
112 process.versions.node,
113 "^20.19.0 || >=22.0.0 <22.14.0",
114 );
115 if (nodeVersionThrows) {
116 expect(runner.listeners, "to throw");
117 } else {
118 expect(runner.listeners(), "to be empty");
119 }
120 });
121 });
122
123 describe("on any other event listened for", function () {
124 it("should populate its `events` array with SerializableEvents", function () {
125 const suite = {
126 title: "some suite",
127 };
128 const test = {
129 title: "some test",
130 };
131 runner.emit(EVENT_SUITE_BEGIN, suite);
132 runner.emit(EVENT_TEST_BEGIN, test);
133 runner.emit(EVENT_TEST_PASS, test);
134 runner.emit(EVENT_TEST_END, test);
135 runner.emit(EVENT_SUITE_END, suite);
136 expect(reporter.events, "to equal", [
137 {
138 eventName: EVENT_SUITE_BEGIN,
139 data: suite,
140 __type: "MockSerializableEvent",
141 },
142 {
143 eventName: EVENT_TEST_BEGIN,
144 data: test,
145 __type: "MockSerializableEvent",
146 },
147 {
148 eventName: EVENT_TEST_PASS,
149 data: test,
150 __type: "MockSerializableEvent",
151 },
152 {
153 eventName: EVENT_TEST_END,
154 data: test,
155 __type: "MockSerializableEvent",
156 },
157 {
158 eventName: EVENT_SUITE_END,
159 data: suite,
160 __type: "MockSerializableEvent",
161 },
162 ]);
163 });
164 });
165 });
166
167 describe("instance method", function () {
168 let reporter;
169
170 beforeEach(function () {
171 reporter = new ParallelBuffered(runner);
172 });
173
174 describe("done", function () {
175 it("should execute its callback with a SerializableWorkerResult", function () {
176 const suite = {
177 title: "some suite",
178 };
179 const test = {
180 title: "some test",
181 };
182 runner.emit(EVENT_SUITE_BEGIN, suite);
183 runner.emit(EVENT_TEST_BEGIN, test);
184 runner.emit(EVENT_TEST_PASS, test);
185 runner.emit(EVENT_TEST_END, test);
186 runner.emit(EVENT_SUITE_END, suite);
187 const cb = sinon.stub();
188 reporter.done(0, cb);
189 expect(cb, "to have a call satisfying", [
190 {
191 events: [
192 {
193 eventName: EVENT_SUITE_BEGIN,
194 data: suite,
195 __type: "MockSerializableEvent",
196 },
197 {
198 eventName: EVENT_TEST_BEGIN,
199 data: test,
200 __type: "MockSerializableEvent",
201 },
202 {
203 eventName: EVENT_TEST_PASS,
204 data: test,
205 __type: "MockSerializableEvent",
206 },
207 {
208 eventName: EVENT_TEST_END,
209 data: test,
210 __type: "MockSerializableEvent",
211 },
212 {
213 eventName: EVENT_SUITE_END,
214 data: suite,
215 __type: "MockSerializableEvent",
216 },
217 ],
218 failures: 0,
219 __type: "MockSerializableWorkerResult",
220 },
221 ]);
222 });
223
224 it("should reset its `events` prop", function () {
225 const suite = {
226 title: "some suite",
227 };
228 const test = {
229 title: "some test",
230 };
231 runner.emit(EVENT_SUITE_BEGIN, suite);
232 runner.emit(EVENT_TEST_BEGIN, test);
233 runner.emit(EVENT_TEST_PASS, test);
234 runner.emit(EVENT_TEST_END, test);
235 runner.emit(EVENT_SUITE_END, suite);
236 const cb = sinon.stub();
237 reporter.done(0, cb);
238 expect(reporter.events, "to be empty");
239 });
240 });
241 });
242 });