Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker,coverage: support V8 coverage generation #22928

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/internal/process/coverage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
const path = require('path');
const { mkdirSync, writeFileSync } = require('fs');
// TODO(addaleax): add support for coverage to worker threads.
const hasInspector = process.config.variables.v8_enable_inspector === 1 &&
require('internal/worker').isMainThread;
const hasInspector = process.config.variables.v8_enable_inspector === 1;
let inspector = null;
if (hasInspector) inspector = require('inspector');

Expand All @@ -14,7 +12,9 @@ function writeCoverage() {
return;
}

const filename = `coverage-${process.pid}-${Date.now()}.json`;
const { threadId } = require('internal/worker');

const filename = `coverage-${process.pid}-${Date.now()}-${threadId}.json`;
try {
// TODO(bcoe): switch to mkdirp once #22302 is addressed.
mkdirSync(process.env.NODE_V8_COVERAGE);
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/v8-coverage/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
const { Worker } = require('worker_threads');
const path = require('path');

new Worker(path.resolve(__dirname, 'subprocess.js'));
17 changes: 17 additions & 0 deletions test/parallel/test-v8-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ function nextdir() {
assert.strictEqual(fixtureCoverage.functions[2].ranges[1].count, 0);
}

// outputs coverage from worker.
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 thanks for seeing this through @addaleax

const coverageDirectory = path.join(tmpdir.path, nextdir());
const output = spawnSync(process.execPath, [
'--experimental-worker',
require.resolve('../fixtures/v8-coverage/worker')
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
assert.strictEqual(output.status, 0);
const fixtureCoverage = getFixtureCoverage('subprocess.js',
coverageDirectory);
assert.ok(fixtureCoverage);
// first branch executed.
assert.strictEqual(fixtureCoverage.functions[2].ranges[0].count, 1);
// second branch did not execute.
assert.strictEqual(fixtureCoverage.functions[2].ranges[1].count, 0);
}

// does not output coverage if NODE_V8_COVERAGE is empty.
{
const coverageDirectory = path.join(tmpdir.path, nextdir());
Expand Down