diff --git a/test/common/assertSnapshot.js b/test/common/assertSnapshot.js index 9a7482020e098a..d1ec30bfb78c6a 100644 --- a/test/common/assertSnapshot.js +++ b/test/common/assertSnapshot.js @@ -234,13 +234,30 @@ function replaceJunitDuration(str) { // This transform picks only the first line and then the lines from the test // file. function pickTestFileFromLcov(str) { + const expectedFile = 'output.js'; const lines = str.split(/\n/); const firstLineOfTestFile = lines.findIndex( - (line) => line.startsWith('SF:') && line.trim().endsWith('output.js'), + (line) => line.startsWith('SF:') && line.trim().endsWith(expectedFile), ); + + if (firstLineOfTestFile === -1) { + assert.fail( + `Could not find LCOV source record ending with ${expectedFile} ` + + `in LCOV output:\n${str || ''}`, + ); + } + const lastLineOfTestFile = lines.findIndex( (line, index) => index > firstLineOfTestFile && line.trim() === 'end_of_record', ); + + if (lastLineOfTestFile === -1) { + assert.fail( + `Could not find end_of_record for LCOV source record ending with ${expectedFile} ` + + `in LCOV output:\n${str}`, + ); + } + return ( lines[0] + '\n' + lines.slice(firstLineOfTestFile, lastLineOfTestFile + 1).join('\n') + '\n' ); diff --git a/test/fixtures/test-runner/output/lcov_reporter.js b/test/fixtures/test-runner/output/lcov_reporter.js index 832b5d2247965c..97919cb5c4c81f 100644 --- a/test/fixtures/test-runner/output/lcov_reporter.js +++ b/test/fixtures/test-runner/output/lcov_reporter.js @@ -3,7 +3,7 @@ require('../../../common'); const fixtures = require('../../../common/fixtures'); const spawn = require('node:child_process').spawn; -spawn( +const child = spawn( process.execPath, [ '--no-warnings', @@ -14,3 +14,16 @@ spawn( ], { stdio: 'inherit' }, ); + +child.on('error', (err) => { + throw err; +}); + +child.on('exit', (code, signal) => { + if (signal) { + process.kill(process.pid, signal); + return; + } + + process.exitCode = code ?? 1; +});