Skip to content

Commit 3081ff9

Browse files
committed
feat: support no trace
1 parent a3f4954 commit 3081ff9

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "junit-to-ctrf",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

src/convert.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ interface JUnitTestCase {
88
classname: string;
99
name: string;
1010
time: string;
11-
failure?: string;
12-
error?: string;
11+
hasFailure: boolean;
12+
failureTrace: string | undefined,
13+
failureMessage: string | undefined,
14+
failureType: string | undefined,
15+
hasError: boolean,
16+
errorTrace: string | undefined,
17+
errorMessage: string | undefined,
18+
errorType: string | undefined,
1319
skipped?: boolean;
1420
}
1521

1622
async function parseJUnitReport(filePath: string): Promise<JUnitTestCase[]> {
17-
console.log('Reading JUnit report file:', filePath);
23+
console.log('Reading JUnit report file:', filePath);
1824
const xml = await fs.readFile(filePath, 'utf-8');
1925
const result = await xml2js.parseStringPromise(xml);
2026
const testCases: JUnitTestCase[] = [];
@@ -23,16 +29,31 @@ async function parseJUnitReport(filePath: string): Promise<JUnitTestCase[]> {
2329
if (suite.testcase) {
2430
suite.testcase.forEach((testCase: any) => {
2531
const { classname, name, time } = testCase.$;
26-
const failure = testCase.failure ? testCase.failure[0] : undefined;
27-
const error = testCase.error ? testCase.error[0] : undefined;
32+
33+
const hasFailure = testCase.failure !== undefined;
34+
const failureTrace = hasFailure ? (testCase.failure[0]?._ || '') : undefined;
35+
const failureMessage = hasFailure ? (testCase.failure[0]?.$?.message || '') : undefined;
36+
const failureType = hasFailure ? (testCase.failure[0]?.$?.type || '') : undefined;
37+
38+
const hasError = testCase.error !== undefined;
39+
const errorTrace = hasError ? (testCase.error[0]?._ || '') : undefined;
40+
const errorMessage = hasError ? (testCase.error[0]?.$?.message || '') : undefined;
41+
const errorType = hasError ? (testCase.error[0]?.$?.type || '') : undefined;
42+
2843
const skipped = testCase.skipped !== undefined;
2944
testCases.push({
3045
suite: suiteName,
3146
classname,
3247
name,
3348
time,
34-
failure: failure ? (typeof failure === 'string' ? failure : failure._) : undefined,
35-
error: error ? (typeof error === 'string' ? error : error._) : undefined,
49+
hasFailure,
50+
failureTrace,
51+
failureMessage,
52+
failureType,
53+
hasError,
54+
errorTrace,
55+
errorMessage,
56+
errorType,
3657
skipped,
3758
});
3859
});
@@ -64,17 +85,17 @@ async function parseJUnitReport(filePath: string): Promise<JUnitTestCase[]> {
6485
function convertToCTRFTest(testCase: JUnitTestCase, useSuiteName: boolean): CtrfTest {
6586
let status: CtrfTest['status'] = 'other';
6687

67-
if (testCase.failure) {
88+
if (testCase.hasFailure) {
6889
status = 'failed';
69-
} else if (testCase.error) {
90+
} else if (testCase.hasError) {
7091
status = 'failed';
7192
} else if (testCase.skipped) {
7293
status = 'skipped';
7394
} else {
7495
status = 'passed';
7596
}
7697

77-
const durationMs = Math.round(parseFloat(testCase.time) * 1000);
98+
const durationMs = Math.round(parseFloat(testCase.time || '0') * 1000);
7899

79100
const testName = useSuiteName
80101
? `${testCase.suite}: ${testCase.name}`
@@ -84,9 +105,9 @@ function convertToCTRFTest(testCase: JUnitTestCase, useSuiteName: boolean): Ctrf
84105
name: testName,
85106
status,
86107
duration: durationMs,
87-
message: testCase.failure || testCase.error ? (testCase.failure || testCase.error) : undefined,
88-
trace: testCase.failure || testCase.error ? (testCase.failure || testCase.error) : undefined,
89-
suite: testCase.suite || ''
108+
message: testCase.failureMessage || testCase.errorMessage || undefined,
109+
trace: testCase.failureTrace || testCase.errorTrace || undefined,
110+
suite: testCase.suite || '',
90111
};
91112
}
92113

@@ -110,8 +131,8 @@ function createCTRFReport(
110131
skipped,
111132
pending,
112133
other,
113-
start: 0,
114-
stop: 0,
134+
start: 0,
135+
stop: 0,
115136
};
116137

117138
const tool: Tool = {
@@ -150,6 +171,6 @@ export async function convertJUnitToCTRF(
150171
const outputDir = path.dirname(finalOutputPath);
151172
await fs.ensureDir(outputDir);
152173

153-
console.log('Writing CTRF report to:', finalOutputPath);
174+
console.log('Writing CTRF report to:', finalOutputPath);
154175
await fs.outputJson(finalOutputPath, ctrfReport, { spaces: 2 });
155176
}

0 commit comments

Comments
 (0)