From 735901428f9654ebe6f5f039f075f0ed27cb1360 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Mon, 12 Feb 2024 16:32:59 -0800 Subject: [PATCH] chore: Improve JUnit generation (#273) * chore: Improve JUnit generation * try to fix e2e test :-/ * Revert "try to fix e2e test :-/" This reverts commit f23b9d357ae88d0342f1eee71072a4fd4424c66a. * revise the warning msg * add more warn msg * improve warning msg --- src/custom-reporter.ts | 3 +-- src/cypress-runner.ts | 6 +++--- src/sauce-reporter.ts | 15 ++++++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/custom-reporter.ts b/src/custom-reporter.ts index ac0ae03c..b36b912b 100644 --- a/src/custom-reporter.ts +++ b/src/custom-reporter.ts @@ -1,5 +1,5 @@ /** - * This is an extension of MochaJunitReporter + * This is an extension of mocha-junit-reporter */ import xml from 'xml'; @@ -345,7 +345,6 @@ MochaJUnitReporter.prototype.getSauceTestsuiteData = function (suite: any) { }; MochaJUnitReporter.prototype.getSauceTestcaseData = function (testcase: any) { - // console.log(testcase); return { id: testcase.order, screenshot: 0, diff --git a/src/cypress-runner.ts b/src/cypress-runner.ts index 0a083d83..0a382cf6 100644 --- a/src/cypress-runner.ts +++ b/src/cypress-runner.ts @@ -1,4 +1,4 @@ -import { mergeJunitFile } from './sauce-reporter'; +import { mergeJUnitFile } from './sauce-reporter'; import path from 'path'; import fs from 'fs'; import { @@ -38,7 +38,7 @@ async function report( } try { - mergeJunitFile( + mergeJUnitFile( specFiles, runCfg.resultsDir, suiteName, @@ -46,7 +46,7 @@ async function report( platformName, ); } catch (e) { - console.error('Failed to generate junit file:', e); + console.warn('Skipping JUnit file generation:', e); } try { diff --git a/src/sauce-reporter.ts b/src/sauce-reporter.ts index d9b969b0..b23ab4f1 100644 --- a/src/sauce-reporter.ts +++ b/src/sauce-reporter.ts @@ -4,7 +4,7 @@ import { escapeXML } from 'sauce-testrunner-utils'; import convert from 'xml-js'; import { XmlSuiteContainer } from './types'; -export function mergeJunitFile( +export function mergeJUnitFile( specFiles: any[], resultsFolder: string, testName: string, @@ -22,16 +22,21 @@ export function mergeJunitFile( } = { compact: true, spaces: 4 }; const testsuites = []; for (let i = 0; i < specFiles.length; i++) { - const xmlData = fs.readFileSync( - path.join(resultsFolder, `${specFiles[i]}.xml`), - 'utf8', - ); + const specJUnitFile = path.join(resultsFolder, `${specFiles[i]}.xml`); + if (!fs.existsSync(specJUnitFile)) { + console.warn( + `JUnit file not found for spec: ${specFiles[i]}. Proceeding without it...`, + ); + continue; + } + const xmlData = fs.readFileSync(specJUnitFile, 'utf8'); const jsObj = convert.xml2js(xmlData, opts) as XmlSuiteContainer; if (jsObj.testsuites && jsObj.testsuites.testsuite) { testsuites.push(...jsObj.testsuites.testsuite); } } if (testsuites.length === 0) { + console.warn('JUnit file generation skipped: no test suites detected.'); return; }