Skip to content

Commit

Permalink
feat: parse v2 to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep committed Aug 17, 2024
1 parent 0ab81fe commit c071b6a
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 6 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
"xUnit",
"TestNG",
"parser",
"parse",
"test",
"testing",
"results",
"result",
"report",
"automation",
"mocha",
"cucumber",
"nUnit",
"MSTest",
"testbeats"
],
"author": "Anudeep <[email protected]>",
Expand Down
65 changes: 62 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
import TestResult from "./models/TestResult";

declare interface ParseOptions {
export interface ITestResult {
name: string;
total: number;
passed: number;
failed: number;
errors: number;
skipped: number;
retried: number;
duration: number;
status: string;
tags: string[];
metadata: object;
suites: ITestSuite[];
}

export interface ITestSuite {
name: string;
total: number;
passed: number;
failed: number;
errors: number;
skipped: number;
duration: number;
status: string;
tags: string[];
metadata: object;
cases: ITestCase[];
}

export interface ITestCase {
name: string;
total: number;
passed: number;
failed: number;
errors: number;
skipped: number;
duration: number;
status: string;
failure: string;
stack_trace: string;
tags: string[];
metadata: object;
steps: ITestStep[];
attachments: ITestAttachment[];
}

export interface ITestStep {
name: string;
duration: number;
status: string;
failure: string;
stack_trace: string;
}

export interface ITestAttachment {
name: string;
path: string;
}

export interface ParseOptions {
type: string;
files: string[];
}

export function parse(options: ParseOptions): TestResult;
export function parse(options: ParseOptions): ITestResult;
export function parseV2(options: ParseOptions): { result: ITestResult, errors: string[] };

export namespace parser { }
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ function parse(options) {
return parser.parse(options);
}

function parseV2(options) {
return parser.parseV2(options);
}

module.exports = {
parse
parse,
parseV2
}
25 changes: 24 additions & 1 deletion src/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ function parse(options) {
return merge(results);
}

function parseV2(options) {
const parser = getParser(options.type);
const results = [];
const errors = [];
for (let i = 0; i < options.files.length; i++) {
const matched_files = getMatchingFilePaths(options.files[i]);
for (let j = 0; j < matched_files.length; j++) {
const file = matched_files[j];
try {
results.push(parser.parse(file, options));
} catch (error) {
errors.push(error.toString());
console.error(error);
}
}
}
if (results.length > 0) {
return { result: merge(results), errors: errors };
}
return { result: null, errors: errors };
}

module.exports = {
parse
parse,
parseV2
}
2 changes: 1 addition & 1 deletion tests/parser.junit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,6 @@ describe('Parser - JUnit', () => {
assert.equal(result.status, 'FAIL');
assert.equal(result.suites[1].cases[1].attachments[0].name, `test-failed-1.png`);
assert.equal(result.suites[1].cases[1].attachments[0].path, `example-get-started-link-chromium/test-failed-1.png`);
})
});

});
38 changes: 38 additions & 0 deletions tests/parser.v2.junit.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { parseV2 } = require('../src');
const assert = require('assert');

describe('Parser V2 - JUnit', () => {

const testDataPath = "tests/data/junit"

it('with all files valid', () => {
const { result } = parseV2({ type: 'junit', ignore_error_count: true, files: [`${testDataPath}/playwright-failures.xml`]});
assert.equal(result.total, 16);
assert.equal(result.passed, 14);
assert.equal(result.failed, 2);
assert.equal(result.status, 'FAIL');
assert.equal(result.suites[1].cases[1].attachments[0].name, `test-failed-1.png`);
assert.equal(result.suites[1].cases[1].attachments[0].path, `example-get-started-link-chromium/test-failed-1.png`);
});

it('with one invalid file ', () => {
const { result, errors } = parseV2({ type: 'junit', ignore_error_count: true, files: [`${testDataPath}/playwright-failures.xml`, `${testDataPath}/playwright-failures.json`] });
assert.equal(result.total, 16);
assert.equal(result.passed, 14);
assert.equal(result.failed, 2);
assert.equal(result.status, 'FAIL');
assert.equal(result.suites[1].cases[1].attachments[0].name, `test-failed-1.png`);
assert.equal(result.suites[1].cases[1].attachments[0].path, `example-get-started-link-chromium/test-failed-1.png`);
assert.ok(errors.length === 1);
assert.ok(errors[0].includes(`Error`));
});

it('with all files invalid', () => {
const { result, errors } = parseV2({ type: 'junit', ignore_error_count: true, files: [`${testDataPath}/invalid.xml`, `${testDataPath}/invalid.json`] });
assert.equal(result, null);
assert.ok(errors.length === 2);
assert.ok(errors[0].includes(`Error`));
assert.ok(errors[1].includes(`Error`));
});

});

0 comments on commit c071b6a

Please sign in to comment.