Skip to content

Coverage file writing hook is called too early during 'exit'. #11445

@Fishrock123

Description

@Fishrock123

It has come to my attention while I was looking through coverage.nodejs.org that my commit intended to add coverage of a specific if branch in process.nextTick() was still now showing up in in the coverage reports.

The commit is f65a48f and the relevant lines can be found at

// on the way out, don't bother. it won't get fired anyway.
if (process._exiting)
return;
.

It appears that in the coverage setup, the exit handler is added too early (that is, before test code is able to attach exit handlers (or at least in all possible cases)):

function setup() {
const reallyReallyExit = process.reallyExit;
process.reallyExit = function(code) {
writeCoverage();
reallyReallyExit(code);
};
process.on('exit', writeCoverage);
}

I tried to patch this a couple ways but I wasn't able to figure out the ideal way to make this exit listener always fire last without adding a new C++ hook.

Here is a diff that I think should work, but just appears to make things worse:

diff --git a/lib/internal/process/write-coverage.js b/lib/internal/process/write-coverage.js
index 6bbc59a..be06a5e 100644
--- a/lib/internal/process/write-coverage.js
+++ b/lib/internal/process/write-coverage.js
@@ -40,7 +40,11 @@ function setup() {
     reallyReallyExit(code);
   };
 
-  process.on('exit', writeCoverage);
+  process.on('exit', () => {
+    process.on('exit', () => {
+      process.on('exit', writeCoverage);
+    });
+  });
 }
 
 exports.setup = setup;

Refs: #10856

cc @CurryKitten, @mhdawson, @addaleax

Metadata

Metadata

Assignees

No one assigned

    Labels

    buildIssues and PRs related to build files or the CI.testIssues and PRs related to the tests.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions