-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
chore: fix e2e test reports #21354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jvbriones
wants to merge
6
commits into
main
Choose a base branch
from
chore-test-reports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+423
−139
Open
chore: fix e2e test reports #21354
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dbbad1f
chore: fix e2e test reports
jvbriones 5139aef
chore: fix e2e test reports
jvbriones 7d44c34
chore: fix e2e test reports
jvbriones 4481f2f
chore: fix e2e test reports
jvbriones a7221c3
chore: fix e2e test reports
jvbriones 5c2566e
chore: fix e2e test reports
jvbriones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
File renamed without changes.
This file contains hidden or 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,291 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Merges multiple junit XML reports into a single deduplicated report. | ||
* | ||
* For test cases that appear in multiple reports (retries), keeps only the | ||
* LAST run result based on file timestamp. This means: | ||
* - If a test fails initially but passes on retry, it shows as PASSED | ||
* - If a test passes initially but fails on retry, it shows as FAILED | ||
* - Only the most recent result is included in the final report | ||
* | ||
* This is useful when Detox retries failed tests, creating multiple XML | ||
* files per test run, and we want the final report to reflect the actual | ||
* outcome after all retries. | ||
* | ||
* Process: | ||
* 1) Find all junit-*.xml files in e2e/reports directory | ||
* 2) Parse and group test cases by unique key (suiteName::testName::className) | ||
* 3) Keep only the latest result for each test case (overwrites earlier results) | ||
* 4) Recalculate suite statistics based on deduplicated results | ||
* 5) Write merged report to junit.xml | ||
*/ | ||
|
||
import { readdir, readFile, writeFile } from 'fs/promises'; | ||
import { join } from 'path'; | ||
import xml2js from 'xml2js'; | ||
|
||
const env = { | ||
REPORTS_DIR: process.env.E2E_REPORTS_DIR || './e2e/reports', | ||
OUTPUT_FILE: process.env.E2E_OUTPUT_FILE || 'junit.xml', | ||
}; | ||
|
||
const xmlParser = new xml2js.Parser(); | ||
const xmlBuilder = new xml2js.Builder({ | ||
xmldec: { version: '1.0', encoding: 'UTF-8' }, | ||
renderOpts: { pretty: true, indent: ' ' }, | ||
}); | ||
|
||
/** | ||
* Extract timestamp from filename (e.g., junit-2025-10-17T12-18-43-667Z.xml) | ||
* @param {string} filename - The filename to extract timestamp from | ||
* @returns {string} Sortable timestamp string | ||
*/ | ||
function extractTimestamp(filename) { | ||
const match = filename.match(/junit-(.+)\.xml$/); | ||
return match ? match[1] : ''; | ||
} | ||
|
||
/** | ||
* Find all junit XML files in the reports directory | ||
* @returns {Promise<string[]>} Sorted array of XML filenames | ||
*/ | ||
async function findJUnitXmlFiles() { | ||
try { | ||
const files = await readdir(env.REPORTS_DIR); | ||
const xmlFiles = files | ||
.filter(f => f.startsWith('junit-') && f.endsWith('.xml')) | ||
.sort((a, b) => extractTimestamp(a).localeCompare(extractTimestamp(b))); | ||
|
||
return xmlFiles; | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
console.warn(`⚠️ Reports directory not found: ${env.REPORTS_DIR}`); | ||
return []; | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Parse a junit XML file | ||
* @param {string} filePath - Path to the XML file | ||
* @returns {Promise<Object>} Parsed XML data | ||
*/ | ||
async function parseJUnitXML(filePath) { | ||
const xmlContent = await readFile(filePath, 'utf-8'); | ||
return await xmlParser.parseStringPromise(xmlContent); | ||
} | ||
|
||
/** | ||
* Parse all XML files and combine test cases, keeping only the LATEST result for each test | ||
* @returns {Promise<Object|null>} Merged report data or null if no files found | ||
*/ | ||
async function parseAndMergeReports() { | ||
console.log('🔍 Scanning for junit XML files...'); | ||
|
||
const xmlFiles = await findJUnitXmlFiles(); | ||
|
||
if (xmlFiles.length === 0) { | ||
console.log('⚠️ No junit XML files found to merge'); | ||
return null; | ||
} | ||
|
||
console.log(`📁 Found ${xmlFiles.length} report file(s):`); | ||
xmlFiles.forEach(f => console.log(` - ${f}`)); | ||
|
||
// Map to store LATEST result for each test case | ||
// Key: "suiteName::testcaseName::classname" | ||
// Value: { testcase, suiteName, suiteAttrs, timestamp } | ||
const testCaseMap = new Map(); | ||
const suitePropertiesMap = new Map(); | ||
|
||
// Process each XML file in chronological order (earliest to latest) | ||
for (const xmlFile of xmlFiles) { | ||
const filePath = join(env.REPORTS_DIR, xmlFile); | ||
const timestamp = extractTimestamp(xmlFile); | ||
|
||
console.log(`\n📄 Processing: ${xmlFile}`); | ||
|
||
try { | ||
const parsed = await parseJUnitXML(filePath); | ||
|
||
if (!parsed.testsuites || !parsed.testsuites.testsuite) { | ||
console.log(' ⚠️ No test suites found, skipping'); | ||
continue; | ||
} | ||
|
||
const testsuites = Array.isArray(parsed.testsuites.testsuite) | ||
? parsed.testsuites.testsuite | ||
: [parsed.testsuites.testsuite]; | ||
|
||
for (const testsuite of testsuites) { | ||
const suiteName = testsuite.$.name; | ||
|
||
if (!testsuite.testcase) { | ||
console.log(` ⚠️ Suite "${suiteName}" has no test cases`); | ||
continue; | ||
} | ||
|
||
const testcases = Array.isArray(testsuite.testcase) | ||
? testsuite.testcase | ||
: [testsuite.testcase]; | ||
|
||
console.log(` 📦 Suite: "${suiteName}" (${testcases.length} test(s))`); | ||
|
||
for (const testcase of testcases) { | ||
const testName = testcase.$.name; | ||
const className = testcase.$.classname || suiteName; | ||
const key = `${suiteName}::${testName}::${className}`; | ||
|
||
const isFailure = !!testcase.failure; | ||
const isError = !!testcase.error; | ||
const isSkipped = !!testcase.skipped; | ||
|
||
// Check if we've seen this test before | ||
const existing = testCaseMap.get(key); | ||
const isRetry = !!existing; | ||
|
||
// Always keep the LATEST result (overwrite previous) | ||
testCaseMap.set(key, { | ||
testcase, | ||
suiteName, | ||
suiteAttrs: testsuite.$, | ||
timestamp, | ||
}); | ||
|
||
const statusIcon = isFailure ? '❌' : isError ? '⚠️' : isSkipped ? '⊘' : '✅'; | ||
const retryLabel = isRetry ? ' (retry)' : ''; | ||
console.log(` ${statusIcon} ${testName}${retryLabel}`); | ||
} | ||
|
||
// Track properties (use latest) | ||
if (testsuite.properties) { | ||
suitePropertiesMap.set(suiteName, testsuite.properties); | ||
} | ||
} | ||
} catch (error) { | ||
console.warn(`⚠️ Failed to process ${xmlFile}: ${error.message}`); | ||
continue; | ||
} | ||
} | ||
|
||
console.log('\n🔄 Building deduplicated report with latest results...'); | ||
console.log(` Total unique test cases: ${testCaseMap.size}`); | ||
|
||
// Group test cases by suite name | ||
const suiteMap = new Map(); | ||
|
||
for (const [key, { testcase, suiteName, suiteAttrs }] of testCaseMap) { | ||
if (!suiteMap.has(suiteName)) { | ||
suiteMap.set(suiteName, { | ||
attrs: suiteAttrs, | ||
testcases: [], | ||
properties: suitePropertiesMap.get(suiteName), | ||
}); | ||
} | ||
suiteMap.get(suiteName).testcases.push(testcase); | ||
} | ||
|
||
// Build test suites with recalculated counts | ||
const finalTestSuites = []; | ||
let totalTests = 0; | ||
let totalFailures = 0; | ||
let totalErrors = 0; | ||
let totalSkipped = 0; | ||
let totalTime = 0; | ||
|
||
for (const [suiteName, { attrs, testcases, properties }] of suiteMap) { | ||
// Recalculate suite statistics based on deduplicated test cases | ||
let suiteFailures = 0; | ||
let suiteErrors = 0; | ||
let suiteSkipped = 0; | ||
let suiteTime = 0; | ||
|
||
for (const testcase of testcases) { | ||
if (testcase.failure) suiteFailures++; | ||
if (testcase.error) suiteErrors++; | ||
if (testcase.skipped) suiteSkipped++; | ||
suiteTime += parseFloat(testcase.$.time || '0'); | ||
} | ||
|
||
const suite = { | ||
$: { | ||
...attrs, | ||
name: suiteName, | ||
tests: testcases.length, | ||
failures: suiteFailures, | ||
errors: suiteErrors, | ||
skipped: suiteSkipped, | ||
time: suiteTime.toFixed(3), | ||
}, | ||
testcase: testcases, | ||
}; | ||
|
||
if (properties) { | ||
suite.properties = properties; | ||
} | ||
|
||
finalTestSuites.push(suite); | ||
|
||
totalTests += testcases.length; | ||
totalFailures += suiteFailures; | ||
totalErrors += suiteErrors; | ||
totalSkipped += suiteSkipped; | ||
totalTime += suiteTime; | ||
} | ||
|
||
// Build final XML structure | ||
const mergedReport = { | ||
testsuites: { | ||
$: { | ||
name: 'jest tests', | ||
tests: totalTests, | ||
failures: totalFailures, | ||
errors: totalErrors, | ||
time: totalTime.toFixed(3), | ||
}, | ||
testsuite: finalTestSuites, | ||
}, | ||
}; | ||
|
||
console.log('\n📊 Final Report Summary (after deduplication):'); | ||
console.log(` Total Test Suites: ${finalTestSuites.length}`); | ||
console.log(` Total Tests: ${totalTests}`); | ||
console.log(` Failures: ${totalFailures}`); | ||
console.log(` Errors: ${totalErrors}`); | ||
console.log(` Skipped: ${totalSkipped}`); | ||
console.log(` Total Time: ${totalTime.toFixed(3)}s`); | ||
|
||
return mergedReport; | ||
} | ||
|
||
/** | ||
* Main execution | ||
*/ | ||
async function main() { | ||
console.log('🚀 Starting JUnit report merge...\n'); | ||
|
||
const mergedReport = await parseAndMergeReports(); | ||
|
||
if (!mergedReport) { | ||
console.log('\n⚠️ No reports to merge, skipping output file creation'); | ||
process.exit(0); | ||
} | ||
|
||
// Convert to XML | ||
const xml = xmlBuilder.buildObject(mergedReport); | ||
|
||
// Write merged report | ||
const outputPath = join(env.REPORTS_DIR, env.OUTPUT_FILE); | ||
await writeFile(outputPath, xml, 'utf-8'); | ||
|
||
console.log(`\n✅ Merged report written to: ${outputPath}`); | ||
console.log('🎉 Merge complete!\n'); | ||
} | ||
|
||
main().catch((error) => { | ||
console.error('\n❌ Error merging reports:', error); | ||
process.exit(1); | ||
}); | ||
|
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: JUnit Report Missing Skipped Attribute
The root
testsuites
element in the merged JUnit report is missing theskipped
attribute. AlthoughtotalSkipped
is calculated, it's not included in the final XML output, which creates an inconsistency with individualtestsuite
elements and deviates from standard JUnit XML format.