Skip to content

Commit

Permalink
test_runner: move coverage collection to root.postRun()
Browse files Browse the repository at this point in the history
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: nodejs#47651
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
cjihrig authored and MoLow committed Jul 6, 2023
1 parent 6535ab9 commit 882c612
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const {
ArrayPrototypeForEach,
FunctionPrototypeBind,
PromiseResolve,
SafeMap,
} = primordials;
Expand Down Expand Up @@ -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));
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 882c612

Please sign in to comment.