Skip to content

Commit

Permalink
worker,coverage: support V8 coverage generation
Browse files Browse the repository at this point in the history
PR-URL: #22928
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and targos committed Sep 25, 2018
1 parent 5d70652 commit 7639390
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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.
{
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

0 comments on commit 7639390

Please sign in to comment.