-
Notifications
You must be signed in to change notification settings - Fork 30.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test_runner: add code coverage support to spec reporter
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('node:assert'); | ||
const { spawnSync } = require('node:child_process'); | ||
const { readdirSync } = require('node:fs'); | ||
const { test } = require('node:test'); | ||
const fixtures = require('../common/fixtures'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
tmpdir.refresh(); | ||
|
||
function findCoverageFileForPid(pid) { | ||
const pattern = `^coverage\\-${pid}\\-(\\d{13})\\-(\\d+)\\.json$`; | ||
const regex = new RegExp(pattern); | ||
|
||
return readdirSync(tmpdir.path).find((file) => { | ||
return regex.test(file); | ||
}); | ||
} | ||
|
||
function getCoverageFixtureReport() { | ||
const report = [ | ||
'\u2139 ========= coverage report =========', | ||
'\u2139 file | line % | branch % | funcs % | uncovered lines', | ||
'\u2139 test/fixtures/test-runner/coverage.js | 78.65 | 38.46 | 60.00 | 12, 13, 16, 17, 18, 19, 20, 21, 22, 27, 39, 43, 44, 61, 62, 66, 67, 71, 72', | ||
'\u2139 test/fixtures/test-runner/invalid-tap.js | 100.00 | 100.00 | 100.00 | ', | ||
'\u2139 test/fixtures/v8-coverage/throw.js | 71.43 | 50.00 | 100.00 | 5, 6', | ||
'\u2139 all files | 78.35 | 43.75 | 60.00 |', | ||
'\u2139 ====================================' | ||
].join('\n'); | ||
|
||
if (common.isWindows) { | ||
return report.replaceAll('/', '\\'); | ||
} | ||
|
||
return report; | ||
} | ||
|
||
test('coverage is reported and dumped to NODE_V8_COVERAGE if present', (t) => { | ||
if (!process.features.inspector) { | ||
return; | ||
} | ||
|
||
const fixture = fixtures.path('test-runner', 'coverage.js'); | ||
const args = ['--experimental-test-coverage', '--test-reporter', 'spec', fixture]; | ||
const options = { env: { ...process.env, NODE_V8_COVERAGE: tmpdir.path } }; | ||
const result = spawnSync(process.execPath, args, options); | ||
const report = getCoverageFixtureReport(); | ||
|
||
assert(result.stdout.toString().includes(report)); | ||
assert.strictEqual(result.stderr.toString(), ''); | ||
assert.strictEqual(result.status, 0); | ||
assert(findCoverageFileForPid(result.pid)); | ||
}); | ||
|
||
test('coverage is reported without NODE_V8_COVERAGE present', (t) => { | ||
if (!process.features.inspector) { | ||
return; | ||
} | ||
|
||
const fixture = fixtures.path('test-runner', 'coverage.js'); | ||
const args = ['--experimental-test-coverage', '--test-reporter', 'spec', fixture]; | ||
const result = spawnSync(process.execPath, args); | ||
const report = getCoverageFixtureReport(); | ||
|
||
assert(result.stdout.toString().includes(report)); | ||
assert.strictEqual(result.stderr.toString(), ''); | ||
assert.strictEqual(result.status, 0); | ||
assert(!findCoverageFileForPid(result.pid)); | ||
}); |