Skip to content

Commit 1d82bd3

Browse files
authored
feat: parse v2 to handle errors (#84)
1 parent 0ab81fe commit 1d82bd3

File tree

6 files changed

+134
-6
lines changed

6 files changed

+134
-6
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
"xUnit",
2020
"TestNG",
2121
"parser",
22+
"parse",
2223
"test",
2324
"testing",
2425
"results",
2526
"result",
27+
"report",
2628
"automation",
2729
"mocha",
2830
"cucumber",
2931
"nUnit",
32+
"MSTest",
3033
"testbeats"
3134
],
3235
"author": "Anudeep <[email protected]>",

src/index.d.ts

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,69 @@
1-
import TestResult from "./models/TestResult";
21

3-
declare interface ParseOptions {
2+
export interface ITestResult {
3+
name: string;
4+
total: number;
5+
passed: number;
6+
failed: number;
7+
errors: number;
8+
skipped: number;
9+
retried: number;
10+
duration: number;
11+
status: string;
12+
tags: string[];
13+
metadata: object;
14+
suites: ITestSuite[];
15+
}
16+
17+
export interface ITestSuite {
18+
name: string;
19+
total: number;
20+
passed: number;
21+
failed: number;
22+
errors: number;
23+
skipped: number;
24+
duration: number;
25+
status: string;
26+
tags: string[];
27+
metadata: object;
28+
cases: ITestCase[];
29+
}
30+
31+
export interface ITestCase {
32+
name: string;
33+
total: number;
34+
passed: number;
35+
failed: number;
36+
errors: number;
37+
skipped: number;
38+
duration: number;
39+
status: string;
40+
failure: string;
41+
stack_trace: string;
42+
tags: string[];
43+
metadata: object;
44+
steps: ITestStep[];
45+
attachments: ITestAttachment[];
46+
}
47+
48+
export interface ITestStep {
49+
name: string;
50+
duration: number;
51+
status: string;
52+
failure: string;
53+
stack_trace: string;
54+
}
55+
56+
export interface ITestAttachment {
57+
name: string;
58+
path: string;
59+
}
60+
61+
export interface ParseOptions {
462
type: string;
563
files: string[];
664
}
765

8-
export function parse(options: ParseOptions): TestResult;
66+
export function parse(options: ParseOptions): ITestResult;
67+
export function parseV2(options: ParseOptions): { result: ITestResult, errors: string[] };
968

1069
export namespace parser { }

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ function parse(options) {
44
return parser.parse(options);
55
}
66

7+
function parseV2(options) {
8+
return parser.parseV2(options);
9+
}
10+
711
module.exports = {
8-
parse
12+
parse,
13+
parseV2
914
}

src/parsers/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ function parse(options) {
6868
return merge(results);
6969
}
7070

71+
function parseV2(options) {
72+
const parser = getParser(options.type);
73+
const results = [];
74+
const errors = [];
75+
for (let i = 0; i < options.files.length; i++) {
76+
const matched_files = getMatchingFilePaths(options.files[i]);
77+
for (let j = 0; j < matched_files.length; j++) {
78+
const file = matched_files[j];
79+
try {
80+
results.push(parser.parse(file, options));
81+
} catch (error) {
82+
errors.push(error.toString());
83+
console.error(error);
84+
}
85+
}
86+
}
87+
if (results.length > 0) {
88+
return { result: merge(results), errors: errors };
89+
}
90+
return { result: null, errors: errors };
91+
}
92+
7193
module.exports = {
72-
parse
94+
parse,
95+
parseV2
7396
}

tests/parser.junit.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,6 @@ describe('Parser - JUnit', () => {
604604
assert.equal(result.status, 'FAIL');
605605
assert.equal(result.suites[1].cases[1].attachments[0].name, `test-failed-1.png`);
606606
assert.equal(result.suites[1].cases[1].attachments[0].path, `example-get-started-link-chromium/test-failed-1.png`);
607-
})
607+
});
608608

609609
});

tests/parser.v2.junit.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { parseV2 } = require('../src');
2+
const assert = require('assert');
3+
4+
describe('Parser V2 - JUnit', () => {
5+
6+
const testDataPath = "tests/data/junit"
7+
8+
it('with all files valid', () => {
9+
const { result } = parseV2({ type: 'junit', ignore_error_count: true, files: [`${testDataPath}/playwright-failures.xml`]});
10+
assert.equal(result.total, 16);
11+
assert.equal(result.passed, 14);
12+
assert.equal(result.failed, 2);
13+
assert.equal(result.status, 'FAIL');
14+
assert.equal(result.suites[1].cases[1].attachments[0].name, `test-failed-1.png`);
15+
assert.equal(result.suites[1].cases[1].attachments[0].path, `example-get-started-link-chromium/test-failed-1.png`);
16+
});
17+
18+
it('with one invalid file ', () => {
19+
const { result, errors } = parseV2({ type: 'junit', ignore_error_count: true, files: [`${testDataPath}/playwright-failures.xml`, `${testDataPath}/playwright-failures.json`] });
20+
assert.equal(result.total, 16);
21+
assert.equal(result.passed, 14);
22+
assert.equal(result.failed, 2);
23+
assert.equal(result.status, 'FAIL');
24+
assert.equal(result.suites[1].cases[1].attachments[0].name, `test-failed-1.png`);
25+
assert.equal(result.suites[1].cases[1].attachments[0].path, `example-get-started-link-chromium/test-failed-1.png`);
26+
assert.ok(errors.length === 1);
27+
assert.ok(errors[0].includes(`Error`));
28+
});
29+
30+
it('with all files invalid', () => {
31+
const { result, errors } = parseV2({ type: 'junit', ignore_error_count: true, files: [`${testDataPath}/invalid.xml`, `${testDataPath}/invalid.json`] });
32+
assert.equal(result, null);
33+
assert.ok(errors.length === 2);
34+
assert.ok(errors[0].includes(`Error`));
35+
assert.ok(errors[1].includes(`Error`));
36+
});
37+
38+
});

0 commit comments

Comments
 (0)