Skip to content

Commit 823f286

Browse files
cjihrigdanielleadams
authored andcommitted
test_runner: move coverage collection to root.postRun()
This commit moves code coverage collection from the test harness exit handler to the postRun() function of the root test. This is necessary preparatory work for supporting code coverage with --test. The reason is that --test is implemented on top of run(), and that function calls the root test's postRun() function, which outputs the test summary. This happens before the harness exit handler. PR-URL: #47651 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 0bc273f commit 823f286

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/internal/test_runner/harness.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const {
33
ArrayPrototypeForEach,
4+
FunctionPrototypeBind,
45
PromiseResolve,
56
SafeMap,
67
} = primordials;
@@ -138,7 +139,6 @@ function setup(root) {
138139
createProcessEventHandler('unhandledRejection', root);
139140
const coverage = configureCoverage(root, globalOptions);
140141
const exitHandler = () => {
141-
root.harness.coverage = collectCoverage(root, coverage);
142142
root.postRun(new ERR_TEST_FAILURE(
143143
'Promise resolution is still pending but the event loop has already resolved',
144144
kCancelledByParent));
@@ -165,7 +165,7 @@ function setup(root) {
165165
root.harness = {
166166
__proto__: null,
167167
bootstrapComplete: false,
168-
coverage: null,
168+
coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage),
169169
counters: {
170170
__proto__: null,
171171
all: 0,

lib/internal/test_runner/test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,10 @@ class Test extends AsyncResource {
644644
this.reporter.diagnostic(this.nesting, kFilename, `todo ${this.root.harness.counters.todo}`);
645645
this.reporter.diagnostic(this.nesting, kFilename, `duration_ms ${this.#duration()}`);
646646

647-
if (this.harness?.coverage) {
648-
this.reporter.coverage(this.nesting, kFilename, this.harness.coverage);
647+
const coverage = this.harness.coverage();
648+
649+
if (coverage) {
650+
this.reporter.coverage(this.nesting, kFilename, coverage);
649651
}
650652

651653
this.reporter.push(null);

0 commit comments

Comments
 (0)