Skip to content

Commit

Permalink
chore: Improve JUnit generation (#273)
Browse files Browse the repository at this point in the history
* chore: Improve JUnit generation

* try to fix e2e test :-/

* Revert "try to fix e2e test :-/"

This reverts commit f23b9d3.

* revise the warning msg

* add more warn msg

* improve warning msg
  • Loading branch information
tianfeng92 authored Feb 13, 2024
1 parent 496da0a commit 7359014
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/custom-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* This is an extension of MochaJunitReporter
* This is an extension of mocha-junit-reporter
*/

import xml from 'xml';
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/cypress-runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mergeJunitFile } from './sauce-reporter';
import { mergeJUnitFile } from './sauce-reporter';
import path from 'path';
import fs from 'fs';
import {
Expand Down Expand Up @@ -38,15 +38,15 @@ async function report(
}

try {
mergeJunitFile(
mergeJUnitFile(
specFiles,
runCfg.resultsDir,
suiteName,
browserName,
platformName,
);
} catch (e) {
console.error('Failed to generate junit file:', e);
console.warn('Skipping JUnit file generation:', e);
}

try {
Expand Down
15 changes: 10 additions & 5 deletions src/sauce-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}

Expand Down

0 comments on commit 7359014

Please sign in to comment.