From 99482822b9a7f07e70a2da52099370336a56ce35 Mon Sep 17 00:00:00 2001 From: Anudeep Date: Mon, 15 Apr 2024 20:49:07 +0530 Subject: [PATCH] feat: ignore errors wdio junit (#60) --- package-lock.json | 4 +- package.json | 2 +- src/index.d.ts | 1 + src/parsers/index.js | 6 +-- src/parsers/junit.js | 29 ++++++++--- tests/data/junit/wdio-failures-errors.xml | 60 +++++++++++++++++++++++ tests/data/junit/wdio/results-0-0.xml | 2 +- tests/data/junit/wdio/results-0-1.xml | 2 +- tests/parser.junit.spec.js | 20 ++++++++ 9 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 tests/data/junit/wdio-failures-errors.xml diff --git a/package-lock.json b/package-lock.json index 2ceaeb3..4640450 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "test-results-parser", - "version": "0.1.11", + "version": "0.1.12", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "test-results-parser", - "version": "0.1.11", + "version": "0.1.12", "license": "MIT", "dependencies": { "fast-xml-parser": "^4.3.2", diff --git a/package.json b/package.json index 40c4df4..0ba0605 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "test-results-parser", - "version": "0.1.11", + "version": "0.1.12", "description": "Parse test results from JUnit, TestNG, xUnit, cucumber and many more", "main": "src/index.js", "types": "./src/index.d.ts", diff --git a/src/index.d.ts b/src/index.d.ts index 85b16e3..fcfc07e 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -2,6 +2,7 @@ import TestResult from "./models/TestResult"; declare interface ParseOptions { type: string; + ignore_errors?: boolean; files: string[]; } diff --git a/src/parsers/index.js b/src/parsers/index.js index 2054036..da77cfa 100644 --- a/src/parsers/index.js +++ b/src/parsers/index.js @@ -9,7 +9,7 @@ const TestResult = require('../models/TestResult'); const { getMatchingFilePaths } = require('../helpers/helper'); /** - * @param {import('../models/TestResult')[]} results + * @param {import('../models/TestResult')[]} results */ function merge(results) { const main_result = new TestResult(); @@ -53,7 +53,7 @@ function getParser(type) { } /** - * @param {import('../index').ParseOptions} options + * @param {import('../index').ParseOptions} options */ function parse(options) { const parser = getParser(options.type); @@ -62,7 +62,7 @@ function parse(options) { const matched_files = getMatchingFilePaths(options.files[i]); for (let j = 0; j < matched_files.length; j++) { const file = matched_files[j]; - results.push(parser.parse(file)); + results.push(parser.parse(file, options)); } } return merge(results); diff --git a/src/parsers/junit.js b/src/parsers/junit.js index 33addb2..994df3a 100644 --- a/src/parsers/junit.js +++ b/src/parsers/junit.js @@ -21,13 +21,19 @@ function getTestCase(rawCase, suite_meta) { return test_case; } -function getTestSuite(rawSuite) { +/** + * + * @param {object} rawSuite + * @param {import('..').ParseOptions} options + * @returns + */ +function getTestSuite(rawSuite, options) { const suite = new TestSuite(); suite.name = rawSuite["@_name"]; suite.total = rawSuite["@_tests"]; suite.failed = rawSuite["@_failures"]; const errors = rawSuite["@_errors"]; - if (errors) { + if (!options.ignore_errors && errors) { suite.errors = errors; } const skipped = rawSuite["@_skipped"]; @@ -130,16 +136,17 @@ function setAggregateResults(result) { /** * * @param {import('./junit.result').JUnitResultJson} json + * @param {import('..').ParseOptions} options * @returns */ -function getTestResult(json) { +function getTestResult(json, options) { const result = new TestResult(); const rawResult = json["testsuites"] ? json["testsuites"][0] : json["testsuite"]; result.name = rawResult["@_name"] || ''; result.total = rawResult["@_tests"]; result.failed = rawResult["@_failures"]; const errors = rawResult["@_errors"]; - if (errors) { + if (!options.ignore_errors && errors) { result.errors = errors; } const skipped = rawResult["@_skipped"]; @@ -156,12 +163,12 @@ function getTestResult(json) { if (!(typeof rawSuites === "undefined")) { const filteredSuites = rawSuites.filter(suite => suite.testcase); for (let i = 0; i < filteredSuites.length; i++) { - result.suites.push(getTestSuite(filteredSuites[i])); + result.suites.push(getTestSuite(filteredSuites[i], options)); } } } else { // top level element is testsuite - result.suites.push(getTestSuite(rawResult)); + result.suites.push(getTestSuite(rawResult, options)); } setAggregateResults(result); @@ -169,9 +176,15 @@ function getTestResult(json) { return result; } -function parse(file) { +/** + * + * @param {string} file + * @param {import('..').ParseOptions} options + * @returns + */ +function parse(file, options) { const json = getJsonFromXMLFile(file); - return getTestResult(json); + return getTestResult(json, options); } module.exports = { diff --git a/tests/data/junit/wdio-failures-errors.xml b/tests/data/junit/wdio-failures-errors.xml new file mode 100644 index 0000000..a67f2ba --- /dev/null +++ b/tests/data/junit/wdio-failures-errors.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + = 1 +Received: 0 + at default (file:///home/runner/work/rsct/src/support/lib/checkIfElementExists.ts:25:37) + at processTicksAndRejections (node:internal/process/task_queues:95:5) + at async default (file:///home/runner/work/rsct/src/support/action/clickElement.ts:19:5) + at async findDestination (file:///home/runner/work/rsct/src/pages/webapp/destinations.ts:13:5) + at async World.navigateToDestination (file:///home/runner/work/rsct/src/support/utils/webapp/navigateToDestination.ts:7:5) +]]> + + + + + + \ No newline at end of file diff --git a/tests/data/junit/wdio/results-0-0.xml b/tests/data/junit/wdio/results-0-0.xml index d707b38..4abcb39 100644 --- a/tests/data/junit/wdio/results-0-0.xml +++ b/tests/data/junit/wdio/results-0-0.xml @@ -5,7 +5,7 @@ - + - + { assert.equal(result.status, 'PASS'); }); + it('wdio - failures and errors', () => { + const result = parse({ type: 'junit', files: [`${testDataPath}/wdio-failures-errors.xml`] }); + assert.equal(result.total, 4); + assert.equal(result.passed, 0); + assert.equal(result.failed, 2); + assert.equal(result.errors, 2); + assert.equal(result.duration, 91024); + assert.equal(result.status, 'FAIL'); + }); + + it('wdio - failures and ignore errors', () => { + const result = parse({ type: 'junit', ignore_errors: true, files: [`${testDataPath}/wdio-failures-errors.xml`] }); + assert.equal(result.total, 4); + assert.equal(result.passed, 2); + assert.equal(result.failed, 2); + assert.equal(result.errors, 0); + assert.equal(result.duration, 91024); + assert.equal(result.status, 'FAIL'); + }); + }); \ No newline at end of file