diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index c22b961b381468..7392597409feff 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -1,6 +1,7 @@ 'use strict'; const { ArrayPrototypeForEach, + FunctionPrototypeBind, PromiseResolve, SafeMap, } = primordials; @@ -138,7 +139,6 @@ function setup(root) { createProcessEventHandler('unhandledRejection', root); const coverage = configureCoverage(root, globalOptions); const exitHandler = () => { - root.harness.coverage = collectCoverage(root, coverage); root.postRun(new ERR_TEST_FAILURE( 'Promise resolution is still pending but the event loop has already resolved', kCancelledByParent)); @@ -165,7 +165,7 @@ function setup(root) { root.harness = { __proto__: null, bootstrapComplete: false, - coverage: null, + coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage), counters: { __proto__: null, all: 0, diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 0fd74d1b257653..92b9c2cbc083e1 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -644,8 +644,10 @@ class Test extends AsyncResource { this.reporter.diagnostic(this.nesting, kFilename, `todo ${this.root.harness.counters.todo}`); this.reporter.diagnostic(this.nesting, kFilename, `duration_ms ${this.#duration()}`); - if (this.harness?.coverage) { - this.reporter.coverage(this.nesting, kFilename, this.harness.coverage); + const coverage = this.harness.coverage(); + + if (coverage) { + this.reporter.coverage(this.nesting, kFilename, coverage); } this.reporter.push(null);