Skip to content

Commit 5d910ca

Browse files
MoLowdanielleadams
authored andcommitted
test_runner: add enqueue and dequeue events
PR-URL: #48428 Backport-PR-URL: #48684 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent d795c0a commit 5d910ca

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
@@ -1394,6 +1394,16 @@ object, streaming a series of events representing the execution of the tests.
13941394

13951395
Emitted when code coverage is enabled and all tests have completed.
13961396

1397+
### Event: `'test:dequeue'`
1398+
1399+
* `data` {Object}
1400+
* `file` {string|undefined} The path of the test file,
1401+
undefined if test is not ran through a file.
1402+
* `name` {string} The test name.
1403+
* `nesting` {number} The nesting level of the test.
1404+
1405+
Emitted when a test is dequeued, right before it is executed.
1406+
13971407
### Event: `'test:diagnostic'`
13981408

13991409
* `data` {Object}
@@ -1404,6 +1414,16 @@ Emitted when code coverage is enabled and all tests have completed.
14041414

14051415
Emitted when [`context.diagnostic`][] is called.
14061416

1417+
### Event: `'test:enqueue'`
1418+
1419+
* `data` {Object}
1420+
* `file` {string|undefined} The path of the test file,
1421+
undefined if test is not ran through a file.
1422+
* `name` {string} The test name.
1423+
* `nesting` {number} The nesting level of the test.
1424+
1425+
Emitted when a test is enqueued for execution.
1426+
14071427
### Event: `'test:fail'`
14081428

14091429
* `data` {Object}
@@ -1453,7 +1473,9 @@ Emitted when all subtests have completed for a given test.
14531473
* `name` {string} The test name.
14541474
* `nesting` {number} The nesting level of the test.
14551475

1456-
Emitted when a test starts.
1476+
Emitted when a test starts reporting its own and its subtests status.
1477+
This event is guaranteed to be emitted in the same order as the tests are
1478+
defined.
14571479

14581480
### Event: `'test:stderr'`
14591481

lib/internal/test_runner/test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ class Test extends AsyncResource {
313313
async processPendingSubtests() {
314314
while (this.pendingSubtests.length > 0 && this.hasConcurrency()) {
315315
const deferred = ArrayPrototypeShift(this.pendingSubtests);
316-
await deferred.test.run();
316+
const test = deferred.test;
317+
this.reporter.dequeue(test.nesting, kFilename, test.name);
318+
await test.run();
317319
deferred.resolve();
318320
}
319321
}
@@ -471,6 +473,7 @@ class Test extends AsyncResource {
471473
// If there is enough available concurrency to run the test now, then do
472474
// it. Otherwise, return a Promise to the caller and mark the test as
473475
// pending for later execution.
476+
this.reporter.enqueue(this.nesting, kFilename, this.name);
474477
if (!this.parent.hasConcurrency()) {
475478
const deferred = createDeferredPromise();
476479

@@ -479,6 +482,7 @@ class Test extends AsyncResource {
479482
return deferred.promise;
480483
}
481484

485+
this.reporter.dequeue(this.nesting, kFilename, this.name);
482486
return this.run();
483487
}
484488

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)