|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('node:assert'); |
| 4 | +const { spawnSync } = require('node:child_process'); |
| 5 | +const { readdirSync } = require('node:fs'); |
| 6 | +const { test } = require('node:test'); |
| 7 | +const fixtures = require('../common/fixtures'); |
| 8 | +const tmpdir = require('../common/tmpdir'); |
| 9 | + |
| 10 | +tmpdir.refresh(); |
| 11 | + |
| 12 | +function findCoverageFileForPid(pid) { |
| 13 | + const pattern = `^coverage\\-${pid}\\-(\\d{13})\\-(\\d+)\\.json$`; |
| 14 | + const regex = new RegExp(pattern); |
| 15 | + |
| 16 | + return readdirSync(tmpdir.path).find((file) => { |
| 17 | + return regex.test(file); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +function getCoverageFixtureReport() { |
| 22 | + const report = [ |
| 23 | + '\u2139 ========= coverage report =========', |
| 24 | + '\u2139 file | line % | branch % | funcs % | uncovered lines', |
| 25 | + '\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', |
| 26 | + '\u2139 test/fixtures/test-runner/invalid-tap.js | 100.00 | 100.00 | 100.00 | ', |
| 27 | + '\u2139 test/fixtures/v8-coverage/throw.js | 71.43 | 50.00 | 100.00 | 5, 6', |
| 28 | + '\u2139 all files | 78.35 | 43.75 | 60.00 |', |
| 29 | + '\u2139 ====================================' |
| 30 | + ].join('\n'); |
| 31 | + |
| 32 | + if (common.isWindows) { |
| 33 | + return report.replaceAll('/', '\\'); |
| 34 | + } |
| 35 | + |
| 36 | + return report; |
| 37 | +} |
| 38 | + |
| 39 | +test('coverage is reported and dumped to NODE_V8_COVERAGE if present', (t) => { |
| 40 | + if (!process.features.inspector) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + const fixture = fixtures.path('test-runner', 'coverage.js'); |
| 45 | + const args = ['--experimental-test-coverage', '--test-reporter', 'spec', fixture]; |
| 46 | + const options = { env: { ...process.env, NODE_V8_COVERAGE: tmpdir.path } }; |
| 47 | + const result = spawnSync(process.execPath, args, options); |
| 48 | + const report = getCoverageFixtureReport(); |
| 49 | + |
| 50 | + assert(result.stdout.toString().includes(report)); |
| 51 | + assert.strictEqual(result.stderr.toString(), ''); |
| 52 | + assert.strictEqual(result.status, 0); |
| 53 | + assert(findCoverageFileForPid(result.pid)); |
| 54 | +}); |
| 55 | + |
| 56 | +test('coverage is reported without NODE_V8_COVERAGE present', (t) => { |
| 57 | + if (!process.features.inspector) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const fixture = fixtures.path('test-runner', 'coverage.js'); |
| 62 | + const args = ['--experimental-test-coverage', '--test-reporter', 'spec', fixture]; |
| 63 | + const result = spawnSync(process.execPath, args); |
| 64 | + const report = getCoverageFixtureReport(); |
| 65 | + |
| 66 | + assert(result.stdout.toString().includes(report)); |
| 67 | + assert.strictEqual(result.stderr.toString(), ''); |
| 68 | + assert.strictEqual(result.status, 0); |
| 69 | + assert(!findCoverageFileForPid(result.pid)); |
| 70 | +}); |
0 commit comments