Skip to content

feat: support no trace #9

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

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "junit-to-ctrf",
"version": "0.0.6",
"version": "0.0.7",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
53 changes: 37 additions & 16 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ interface JUnitTestCase {
classname: string;
name: string;
time: string;
failure?: string;
error?: string;
hasFailure: boolean;
failureTrace: string | undefined,
failureMessage: string | undefined,
failureType: string | undefined,
hasError: boolean,
errorTrace: string | undefined,
errorMessage: string | undefined,
errorType: string | undefined,
skipped?: boolean;
}

async function parseJUnitReport(filePath: string): Promise<JUnitTestCase[]> {
console.log('Reading JUnit report file:', filePath);
console.log('Reading JUnit report file:', filePath);
const xml = await fs.readFile(filePath, 'utf-8');
const result = await xml2js.parseStringPromise(xml);
const testCases: JUnitTestCase[] = [];
Expand All @@ -23,16 +29,31 @@ async function parseJUnitReport(filePath: string): Promise<JUnitTestCase[]> {
if (suite.testcase) {
suite.testcase.forEach((testCase: any) => {
const { classname, name, time } = testCase.$;
const failure = testCase.failure ? testCase.failure[0] : undefined;
const error = testCase.error ? testCase.error[0] : undefined;

const hasFailure = testCase.failure !== undefined;
const failureTrace = hasFailure ? (testCase.failure[0]?._ || '') : undefined;
const failureMessage = hasFailure ? (testCase.failure[0]?.$?.message || '') : undefined;
const failureType = hasFailure ? (testCase.failure[0]?.$?.type || '') : undefined;

const hasError = testCase.error !== undefined;
const errorTrace = hasError ? (testCase.error[0]?._ || '') : undefined;
const errorMessage = hasError ? (testCase.error[0]?.$?.message || '') : undefined;
const errorType = hasError ? (testCase.error[0]?.$?.type || '') : undefined;

const skipped = testCase.skipped !== undefined;
testCases.push({
suite: suiteName,
classname,
name,
time,
failure: failure ? (typeof failure === 'string' ? failure : failure._) : undefined,
error: error ? (typeof error === 'string' ? error : error._) : undefined,
hasFailure,
failureTrace,
failureMessage,
failureType,
hasError,
errorTrace,
errorMessage,
errorType,
skipped,
});
});
Expand Down Expand Up @@ -64,17 +85,17 @@ async function parseJUnitReport(filePath: string): Promise<JUnitTestCase[]> {
function convertToCTRFTest(testCase: JUnitTestCase, useSuiteName: boolean): CtrfTest {
let status: CtrfTest['status'] = 'other';

if (testCase.failure) {
if (testCase.hasFailure) {
status = 'failed';
} else if (testCase.error) {
} else if (testCase.hasError) {
status = 'failed';
} else if (testCase.skipped) {
status = 'skipped';
} else {
status = 'passed';
}

const durationMs = Math.round(parseFloat(testCase.time) * 1000);
const durationMs = Math.round(parseFloat(testCase.time || '0') * 1000);

const testName = useSuiteName
? `${testCase.suite}: ${testCase.name}`
Expand All @@ -84,9 +105,9 @@ function convertToCTRFTest(testCase: JUnitTestCase, useSuiteName: boolean): Ctrf
name: testName,
status,
duration: durationMs,
message: testCase.failure || testCase.error ? (testCase.failure || testCase.error) : undefined,
trace: testCase.failure || testCase.error ? (testCase.failure || testCase.error) : undefined,
suite: testCase.suite || ''
message: testCase.failureMessage || testCase.errorMessage || undefined,
trace: testCase.failureTrace || testCase.errorTrace || undefined,
suite: testCase.suite || '',
};
}

Expand All @@ -110,8 +131,8 @@ function createCTRFReport(
skipped,
pending,
other,
start: 0,
stop: 0,
start: 0,
stop: 0,
};

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

console.log('Writing CTRF report to:', finalOutputPath);
console.log('Writing CTRF report to:', finalOutputPath);
await fs.outputJson(finalOutputPath, ctrfReport, { spaces: 2 });
}
Loading