| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ee66a38 commit 2f47fb2
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,7 @@ const { fileURLToPath, URL } = require('internal/url'); | |||
| 32 | 32 | const { kMappings, SourceMap } = require('internal/source_map/source_map'); | |
| 33 | 33 | const { | |
| 34 | 34 | codes: { | |
| 35 | + ERR_OPERATION_FAILED, | ||
| 35 | 36 | ERR_SOURCE_MAP_CORRUPT, | |
| 36 | 37 | ERR_SOURCE_MAP_MISSING_SOURCE, | |
| 37 | 38 | }, | |
@@ -332,7 +333,7 @@ class TestCoverage { | |||
| 332 | 333 | } | |
| 333 | 334 | ||
| 334 | 335 | const coverageFile = join(this.coverageDirectory, entry.name); | |
| 335 | - const coverage = JSONParse(readFileSync(coverageFile, 'utf8')); | ||
| 336 | + const coverage = readCoverageFile(coverageFile); | ||
| 336 | 337 | this.mergeCoverage(result, this.mapCoverageWithSourceMap(coverage)); | |
| 337 | 338 | } | |
| 338 | 339 | ||
@@ -508,6 +509,24 @@ class TestCoverage { | |||
| 508 | 509 | } | |
| 509 | 510 | } | |
| 510 | 511 | ||
| 512 | + function readCoverageFile(coverageFile) { | ||
| 513 | + const rawCoverage = readFileSync(coverageFile, 'utf8'); | ||
| 514 | + | ||
| 515 | + if (rawCoverage.length === 0) { | ||
| 516 | + throw new ERR_OPERATION_FAILED(`coverage file is empty: ${coverageFile}`); | ||
| 517 | + } | ||
| 518 | + | ||
| 519 | + try { | ||
| 520 | + return JSONParse(rawCoverage); | ||
| 521 | + } catch (err) { | ||
| 522 | + const error = new ERR_OPERATION_FAILED( | ||
| 523 | + `failed to parse coverage file ${coverageFile}: ${err.message}`, | ||
| 524 | + ); | ||
| 525 | + error.cause = err; | ||
| 526 | + throw error; | ||
| 527 | + } | ||
| 528 | + } | ||
| 529 | + | ||
| 511 | 530 | function toPercentage(covered, total) { | |
| 512 | 531 | return total === 0 ? 100 : (covered / total) * 100; | |
| 513 | 532 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,6 +77,22 @@ function getSpecCoverageFixtureReport() { | |||
| 77 | 77 | return report; | |
| 78 | 78 | } | |
| 79 | 79 | ||
| 80 | + function formatSpawnSyncResult(result) { | ||
| 81 | + return [ | ||
| 82 | + `status: ${result.status}`, | ||
| 83 | + `signal: ${result.signal}`, | ||
| 84 | + `stdout:\n${result.stdout}`, | ||
| 85 | + `stderr:\n${result.stderr}`, | ||
| 86 | + ].join('\n'); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + function assertIncludesReport(result, report) { | ||
| 90 | + assert( | ||
| 91 | + result.stdout.toString().includes(report), | ||
| 92 | + formatSpawnSyncResult(result), | ||
| 93 | + ); | ||
| 94 | + } | ||
| 95 | + | ||
| 80 | 96 | test('test coverage report', async (t) => { | |
| 81 | 97 | await t.test('handles the inspector not being available', (t) => { | |
| 82 | 98 | if (process.features.inspector) { | |
@@ -111,7 +127,7 @@ test('test tap coverage reporter', skipIfNoInspector, async (t) => { | |||
| 111 | 127 | const options = { env: { ...process.env, NODE_V8_COVERAGE: tmpdir.path } }; | |
| 112 | 128 | const result = spawnSync(process.execPath, args, options); | |
| 113 | 129 | const report = getTapCoverageFixtureReport(); | |
| 114 | - assert(result.stdout.toString().includes(report)); | ||
| 130 | + assertIncludesReport(result, report); | ||
| 115 | 131 | assert.strictEqual(result.stderr.toString(), ''); | |
| 116 | 132 | assert.strictEqual(result.status, 0); | |
| 117 | 133 | assert(findCoverageFileForPid(result.pid)); | |
@@ -129,7 +145,7 @@ test('test tap coverage reporter', skipIfNoInspector, async (t) => { | |||
| 129 | 145 | const result = spawnSync(process.execPath, args); | |
| 130 | 146 | const report = getTapCoverageFixtureReport(); | |
| 131 | 147 | ||
| 132 | - assert(result.stdout.toString().includes(report)); | ||
| 148 | + assertIncludesReport(result, report); | ||
| 133 | 149 | assert.strictEqual(result.stderr.toString(), ''); | |
| 134 | 150 | assert.strictEqual(result.status, 0); | |
| 135 | 151 | assert(!findCoverageFileForPid(result.pid)); | |
@@ -149,7 +165,7 @@ test('test spec coverage reporter', skipIfNoInspector, async (t) => { | |||
| 149 | 165 | const result = spawnSync(process.execPath, args, options); | |
| 150 | 166 | const report = getSpecCoverageFixtureReport(); | |
| 151 | 167 | ||
| 152 | - assert(result.stdout.toString().includes(report)); | ||
| 168 | + assertIncludesReport(result, report); | ||
| 153 | 169 | assert.strictEqual(result.stderr.toString(), ''); | |
| 154 | 170 | assert.strictEqual(result.status, 0); | |
| 155 | 171 | assert(findCoverageFileForPid(result.pid)); | |
@@ -166,7 +182,7 @@ test('test spec coverage reporter', skipIfNoInspector, async (t) => { | |||
| 166 | 182 | const result = spawnSync(process.execPath, args); | |
| 167 | 183 | const report = getSpecCoverageFixtureReport(); | |
| 168 | 184 | ||
| 169 | - assert(result.stdout.toString().includes(report)); | ||
| 185 | + assertIncludesReport(result, report); | ||
| 170 | 186 | assert.strictEqual(result.stderr.toString(), ''); | |
| 171 | 187 | assert.strictEqual(result.status, 0); | |
| 172 | 188 | assert(!findCoverageFileForPid(result.pid)); | |
@@ -187,7 +203,7 @@ test('single process coverage is the same with --test', skipIfNoInspector, () => | |||
| 187 | 203 | const report = getTapCoverageFixtureReport(); | |
| 188 | 204 | ||
| 189 | 205 | assert.strictEqual(result.stderr.toString(), ''); | |
| 190 | - assert(result.stdout.toString().includes(report)); | ||
| 206 | + assertIncludesReport(result, report); | ||
| 191 | 207 | assert.strictEqual(result.status, 0); | |
| 192 | 208 | assert(!findCoverageFileForPid(result.pid)); | |
| 193 | 209 | }); | |
@@ -226,7 +242,7 @@ test('coverage is combined for multiple processes', skipIfNoInspector, () => { | |||
| 226 | 242 | }); | |
| 227 | 243 | ||
| 228 | 244 | assert.strictEqual(result.stderr.toString(), ''); | |
| 229 | - assert(result.stdout.toString().includes(report)); | ||
| 245 | + assertIncludesReport(result, report); | ||
| 230 | 246 | assert.strictEqual(result.status, 0); | |
| 231 | 247 | }); | |
| 232 | 248 | ||
@@ -268,7 +284,7 @@ test.skip('coverage works with isolation=none', skipIfNoInspector, common.mustCa | |||
| 268 | 284 | }); | |
| 269 | 285 | ||
| 270 | 286 | assert.strictEqual(result.stderr.toString(), ''); | |
| 271 | - assert(result.stdout.toString().includes(report)); | ||
| 287 | + assertIncludesReport(result, report); | ||
| 272 | 288 | assert.strictEqual(result.status, 0); | |
| 273 | 289 | }, 0)); | |
| 274 | 290 | ||
@@ -285,6 +301,7 @@ test('coverage reports on lines, functions, and branches', skipIfNoInspector, as | |||
| 285 | 301 | ]); | |
| 286 | 302 | assert.strictEqual(child.stderr.toString(), ''); | |
| 287 | 303 | const stdout = child.stdout.toString(); | |
| 304 | + assert.notStrictEqual(stdout, '', formatSpawnSyncResult(child)); | ||
| 288 | 305 | const coverage = JSON.parse(stdout); | |
| 289 | 306 | ||
| 290 | 307 | await t.test('does not include node_modules', () => { | |
@@ -366,7 +383,7 @@ test('coverage with ESM hook - source irrelevant', skipIfNoInspector, () => { | |||
| 366 | 383 | const result = spawnSync(process.execPath, args, { cwd: fixture }); | |
| 367 | 384 | ||
| 368 | 385 | assert.strictEqual(result.stderr.toString(), ''); | |
| 369 | - assert(result.stdout.toString().includes(report)); | ||
| 386 | + assertIncludesReport(result, report); | ||
| 370 | 387 | assert.strictEqual(result.status, 0); | |
| 371 | 388 | }); | |
| 372 | 389 | ||
@@ -401,7 +418,7 @@ test('coverage with ESM hook - source transpiled', skipIfNoInspector, () => { | |||
| 401 | 418 | const result = spawnSync(process.execPath, args, { cwd: fixture }); | |
| 402 | 419 | ||
| 403 | 420 | assert.strictEqual(result.stderr.toString(), ''); | |
| 404 | - assert(result.stdout.toString().includes(report)); | ||
| 421 | + assertIncludesReport(result, report); | ||
| 405 | 422 | assert.strictEqual(result.status, 0); | |
| 406 | 423 | }); | |
| 407 | 424 | ||
@@ -435,7 +452,7 @@ test('coverage with excluded files', skipIfNoInspector, () => { | |||
| 435 | 452 | return report.replaceAll('/', '\\'); | |
| 436 | 453 | } | |
| 437 | 454 | ||
| 438 | - assert(result.stdout.toString().includes(report)); | ||
| 455 | + assertIncludesReport(result, report); | ||
| 439 | 456 | assert.strictEqual(result.status, 0); | |
| 440 | 457 | assert(!findCoverageFileForPid(result.pid)); | |
| 441 | 458 | }); | |
@@ -472,7 +489,7 @@ test('coverage with included files', skipIfNoInspector, () => { | |||
| 472 | 489 | return report.replaceAll('/', '\\'); | |
| 473 | 490 | } | |
| 474 | 491 | ||
| 475 | - assert(result.stdout.toString().includes(report)); | ||
| 492 | + assertIncludesReport(result, report); | ||
| 476 | 493 | assert.strictEqual(result.status, 0); | |
| 477 | 494 | assert(!findCoverageFileForPid(result.pid)); | |
| 478 | 495 | }); | |
@@ -506,7 +523,7 @@ test('coverage with included and excluded files', skipIfNoInspector, () => { | |||
| 506 | 523 | return report.replaceAll('/', '\\'); | |
| 507 | 524 | } | |
| 508 | 525 | ||
| 509 | - assert(result.stdout.toString().includes(report)); | ||
| 526 | + assertIncludesReport(result, report); | ||
| 510 | 527 | assert.strictEqual(result.status, 0); | |
| 511 | 528 | assert(!findCoverageFileForPid(result.pid)); | |
| 512 | 529 | }); | |
@@ -547,7 +564,7 @@ test('correctly prints the coverage report of files contained in parent director | |||
| 547 | 564 | }); | |
| 548 | 565 | ||
| 549 | 566 | assert.strictEqual(result.stderr.toString(), ''); | |
| 550 | - assert(result.stdout.toString().includes(report)); | ||
| 567 | + assertIncludesReport(result, report); | ||
| 551 | 568 | assert.strictEqual(result.status, 0); | |
| 552 | 569 | }); | |
| 553 | 570 | ||
@@ -564,5 +581,5 @@ test('coverage with directory and file named "file"', skipIfNoInspector, () => { | |||
| 564 | 581 | ||
| 565 | 582 | assert.strictEqual(result.stderr.toString(), ''); | |
| 566 | 583 | assert.strictEqual(result.status, 0); | |
| 567 | - assert(result.stdout.toString().includes('start of coverage report')); | ||
| 584 | + assertIncludesReport(result, 'start of coverage report'); | ||
| 568 | 585 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments