Skip to content

Commit 1f120a3

Browse files
cjihrigtargos
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 ccf9c37 commit 1f120a3

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;
@@ -140,7 +141,6 @@ function setup(root) {
140141
createProcessEventHandler('unhandledRejection', root);
141142
const coverage = configureCoverage(root, globalOptions);
142143
const exitHandler = () => {
143-
root.harness.coverage = collectCoverage(root, coverage);
144144
root.postRun(new ERR_TEST_FAILURE(
145145
'Promise resolution is still pending but the event loop has already resolved',
146146
kCancelledByParent));
@@ -167,7 +167,7 @@ function setup(root) {
167167
root.harness = {
168168
__proto__: null,
169169
bootstrapComplete: false,
170-
coverage: null,
170+
coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage),
171171
counters: {
172172
__proto__: null,
173173
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)