diff --git a/src/parsers/junit.js b/src/parsers/junit.js index b733077..0d39c68 100644 --- a/src/parsers/junit.js +++ b/src/parsers/junit.js @@ -96,7 +96,7 @@ function setAggregateResults(result) { */ function getTestResult(json) { const result = new TestResult(); - const rawResult = json["testsuites"][0]; + const rawResult = json["testsuites"] ? json["testsuites"][0] : json["testsuite"]; result.name = rawResult["@_name"] || ''; result.total = rawResult["@_tests"]; result.failed = rawResult["@_failures"]; @@ -111,13 +111,18 @@ function getTestResult(json) { result.total = result.total - result.skipped; result.passed = result.total - result.failed - result.errors; result.duration = rawResult["@_time"] * 1000; - const rawSuites = rawResult["testsuite"]; - if (!(typeof rawSuites === "undefined")) { // Don't filter if there are no testsuite objects - const filteredSuites = rawSuites.filter(suite => suite.testcase); - for (let i = 0; i < filteredSuites.length; i++) { - result.suites.push(getTestSuite(filteredSuites[i])); + if (json["testsuites"]) { // top-level element is testsuites + const rawSuites = rawResult["testsuite"]; + if (!(typeof rawSuites === "undefined")) { // Don't filter if there are no testsuite objects + const filteredSuites = rawSuites.filter(suite => suite.testcase); + for (let i = 0; i < filteredSuites.length; i++) { + result.suites.push(getTestSuite(filteredSuites[i])); + } } + } else { // top level element is testsuite + result.suites.push(getTestSuite(rawResult)); } + setAggregateResults(result); result.status = result.total === result.passed ? 'PASS' : 'FAIL'; return result; diff --git a/tests/data/junit/testCafe.xml b/tests/data/junit/testCafe.xml new file mode 100644 index 0000000..eb8a784 --- /dev/null +++ b/tests/data/junit/testCafe.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/tests/parser.junit.spec.js b/tests/parser.junit.spec.js index 057e842..ae5add7 100644 --- a/tests/parser.junit.spec.js +++ b/tests/parser.junit.spec.js @@ -529,6 +529,13 @@ describe('Parser - JUnit', () => { }); }); + it('parse testcafe with testsuite root node', () => { + const result = parse({ type: 'junit', files: [`${testDataPath}/testCafe.xml`] }); + + assert.equal(result.suites.length, 1); + assert.equal(result.suites[0].cases.length, 2); + }); + it('empty suite with no tests', () => { const result = parse({ type: 'junit', files: [`${testDataPath}/no-suites.xml`] }); assert.deepEqual(result, {