Skip to content

Commit 9eb96bb

Browse files
MoLowpull[bot]
authored andcommitted
test_runner: add enqueue and dequeue events
PR-URL: #48428 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 7b6f497 commit 9eb96bb

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

doc/api/test.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,16 @@ object, streaming a series of events representing the execution of the tests.
14611461

14621462
Emitted when code coverage is enabled and all tests have completed.
14631463

1464+
### Event: `'test:dequeue'`
1465+
1466+
* `data` {Object}
1467+
* `file` {string|undefined} The path of the test file,
1468+
undefined if test is not ran through a file.
1469+
* `name` {string} The test name.
1470+
* `nesting` {number} The nesting level of the test.
1471+
1472+
Emitted when a test is dequeued, right before it is executed.
1473+
14641474
### Event: `'test:diagnostic'`
14651475

14661476
* `data` {Object}
@@ -1471,6 +1481,16 @@ Emitted when code coverage is enabled and all tests have completed.
14711481

14721482
Emitted when [`context.diagnostic`][] is called.
14731483

1484+
### Event: `'test:enqueue'`
1485+
1486+
* `data` {Object}
1487+
* `file` {string|undefined} The path of the test file,
1488+
undefined if test is not ran through a file.
1489+
* `name` {string} The test name.
1490+
* `nesting` {number} The nesting level of the test.
1491+
1492+
Emitted when a test is enqueued for execution.
1493+
14741494
### Event: `'test:fail'`
14751495

14761496
* `data` {Object}
@@ -1520,7 +1540,9 @@ Emitted when all subtests have completed for a given test.
15201540
* `name` {string} The test name.
15211541
* `nesting` {number} The nesting level of the test.
15221542

1523-
Emitted when a test starts.
1543+
Emitted when a test starts reporting its own and its subtests status.
1544+
This event is guaranteed to be emitted in the same order as the tests are
1545+
defined.
15241546

15251547
### Event: `'test:stderr'`
15261548

lib/internal/test_runner/test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ class Test extends AsyncResource {
311311
async processPendingSubtests() {
312312
while (this.pendingSubtests.length > 0 && this.hasConcurrency()) {
313313
const deferred = ArrayPrototypeShift(this.pendingSubtests);
314-
await deferred.test.run();
314+
const test = deferred.test;
315+
this.reporter.dequeue(test.nesting, kFilename, test.name);
316+
await test.run();
315317
deferred.resolve();
316318
}
317319
}
@@ -469,6 +471,7 @@ class Test extends AsyncResource {
469471
// If there is enough available concurrency to run the test now, then do
470472
// it. Otherwise, return a Promise to the caller and mark the test as
471473
// pending for later execution.
474+
this.reporter.enqueue(this.nesting, kFilename, this.name);
472475
if (!this.parent.hasConcurrency()) {
473476
const deferred = createDeferredPromise();
474477

@@ -477,6 +480,7 @@ class Test extends AsyncResource {
477480
return deferred.promise;
478481
}
479482

483+
this.reporter.dequeue(this.nesting, kFilename, this.name);
480484
return this.run();
481485
}
482486

lib/internal/test_runner/tests_stream.js

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ class TestsStream extends Readable {
4949
return { __proto__: null, todo: reason ?? true };
5050
}
5151

52+
enqueue(nesting, file, name) {
53+
this[kEmitMessage]('test:enqueue', { __proto__: null, nesting, file, name });
54+
}
55+
56+
dequeue(nesting, file, name) {
57+
this[kEmitMessage]('test:dequeue', { __proto__: null, nesting, file, name });
58+
}
59+
5260
start(nesting, file, name) {
5361
this[kEmitMessage]('test:start', { __proto__: null, nesting, file, name });
5462
}

test/parallel/test-runner-reporters.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('node:test reporters', { concurrency: true }, () => {
9797
testFile]);
9898
assert.strictEqual(child.stderr.toString(), '');
9999
const stdout = child.stdout.toString();
100-
assert.match(stdout, /{"test:start":4,"test:pass":2,"test:fail":2,"test:plan":2,"test:diagnostic":\d+}$/);
100+
assert.match(stdout, /{"test:enqueue":5,"test:dequeue":5,"test:start":4,"test:pass":2,"test:fail":2,"test:plan":2,"test:diagnostic":\d+}$/);
101101
assert.strictEqual(stdout.slice(0, filename.length + 2), `${filename} {`);
102102
});
103103
});
@@ -109,7 +109,7 @@ describe('node:test reporters', { concurrency: true }, () => {
109109
assert.strictEqual(child.stderr.toString(), '');
110110
assert.match(
111111
child.stdout.toString(),
112-
/^package: reporter-cjs{"test:start":4,"test:pass":2,"test:fail":2,"test:plan":2,"test:diagnostic":\d+}$/,
112+
/^package: reporter-cjs{"test:enqueue":5,"test:dequeue":5,"test:start":4,"test:pass":2,"test:fail":2,"test:plan":2,"test:diagnostic":\d+}$/,
113113
);
114114
});
115115

@@ -120,7 +120,7 @@ describe('node:test reporters', { concurrency: true }, () => {
120120
assert.strictEqual(child.stderr.toString(), '');
121121
assert.match(
122122
child.stdout.toString(),
123-
/^package: reporter-esm{"test:start":4,"test:pass":2,"test:fail":2,"test:plan":2,"test:diagnostic":\d+}$/,
123+
/^package: reporter-esm{"test:enqueue":5,"test:dequeue":5,"test:start":4,"test:pass":2,"test:fail":2,"test:plan":2,"test:diagnostic":\d+}$/,
124124
);
125125
});
126126

0 commit comments

Comments
 (0)