diff --git a/index.js b/index.js index 060ff9d..13ddb16 100644 --- a/index.js +++ b/index.js @@ -194,6 +194,31 @@ function getJenkinsClassname (test, options) { return titles.join(options.suiteTitleSeparatedBy); } +function formatErrorMessage(err, removeInvalidCharacters) { + var message; + if (err.message && typeof err.message.toString === 'function') { + message = err.message + ''; + } else if (typeof err.inspect === 'function') { + message = err.inspect() + ''; + } else { + message = ''; + } + var failureMessage = err.stack || message; + if (!Base.hideDiff && err.expected !== undefined) { + var oldUseColors = Base.useColors; + Base.useColors = false; + failureMessage += "\n" + Base.generateDiff(err.actual, err.expected); + Base.useColors = oldUseColors; + } + return { + _attr: { + message: removeInvalidCharacters(message) || '', + type: err.name || '' + }, + _cdata: removeInvalidCharacters(failureMessage) + }; +} + /** * JUnit reporter for mocha.js. * @module mocha-junit-reporter @@ -330,7 +355,9 @@ MochaJUnitReporter.prototype.getTestcaseData = function(test, err) { _attr: { name: flipClassAndName ? classname : name, time: (typeof test.duration === 'undefined') ? 0 : test.duration / 1000, - classname: flipClassAndName ? name : classname + classname: flipClassAndName ? name : classname, + retries: test._currentRetry, + conclusion: err ? 'failure' : 'success', } }] }; @@ -356,31 +383,14 @@ MochaJUnitReporter.prototype.getTestcaseData = function(test, err) { testcase.testcase.push({'system-err': this.removeInvalidCharacters(stripAnsi(test.consoleErrors.join('\n')))}); } + if (test.prevAttempts) { + const self = this; + test.prevAttempts.forEach(function (attempt) { + testcase.testcase.push({flakyFailure: formatErrorMessage(attempt.err, self.removeInvalidCharacters)}); + }); + } if (err) { - var message; - if (err.message && typeof err.message.toString === 'function') { - message = err.message + ''; - } else if (typeof err.inspect === 'function') { - message = err.inspect() + ''; - } else { - message = ''; - } - var failureMessage = err.stack || message; - if (!Base.hideDiff && err.expected !== undefined) { - var oldUseColors = Base.useColors; - Base.useColors = false; - failureMessage += "\n" + Base.generateDiff(err.actual, err.expected); - Base.useColors = oldUseColors; - } - var failureElement = { - _attr: { - message: this.removeInvalidCharacters(message) || '', - type: err.name || '' - }, - _cdata: this.removeInvalidCharacters(failureMessage) - }; - - testcase.testcase.push({failure: failureElement}); + testcase.testcase.push({failure: formatErrorMessage(err, this.removeInvalidCharacters)}); } return testcase; }; @@ -468,6 +478,7 @@ MochaJUnitReporter.prototype.getXml = function(testsuites) { _suiteAttr.timestamp = new Date(_suiteAttr.timestamp).toISOString().slice(0, -5); _suiteAttr.failures = 0; _suiteAttr.skipped = 0; + _suiteAttr.retries = 0; _cases.forEach(function(testcase) { var lastNode = testcase.testcase[testcase.testcase.length - 1]; @@ -477,6 +488,9 @@ MochaJUnitReporter.prototype.getXml = function(testsuites) { if (typeof testcase.testcase[0]._attr.time === 'number') { testcase.testcase[0]._attr.time = testcase.testcase[0]._attr.time.toFixed(3); } + if (typeof testcase.testcase[0]._attr.retries === 'number') { + _suiteAttr.retries += testcase.testcase[0]._attr.retries; + } }); if (antMode) { diff --git a/test/mock-results.js b/test/mock-results.js index 839ddca..848c5f2 100644 --- a/test/mock-results.js +++ b/test/mock-results.js @@ -21,7 +21,8 @@ module.exports = function(stats, options) { timestamp: "1970-01-01T00:00:00", // ISO timestamp truncated to the second tests: "0", failures: "0", - time: "0.000" + time: "0.000", + retries: "0" } } ] @@ -34,7 +35,8 @@ module.exports = function(stats, options) { timestamp: "1970-01-01T00:00:00", tests: "3", failures: "2", - time: "100.001" + time: "100.001", + retries: "0" } }, { @@ -42,7 +44,9 @@ module.exports = function(stats, options) { _attr: { name: "Foo Bar can weez the juice", classname: "can weez the juice", - time: "0.101" + time: "0.101", + retries: "0", + conclusion: "success" } } }, @@ -52,7 +56,9 @@ module.exports = function(stats, options) { _attr: { name: "Foo Bar can narfle the garthog", classname: "can narfle the garthog", - time: "2.002" + time: "2.002", + retries: "0", + conclusion: "failure" } }, { @@ -72,7 +78,9 @@ module.exports = function(stats, options) { _attr: { name: "Foo Bar can behave like a flandip", classname: "can behave like a flandip", - time: "30.003" + time: "30.003", + retries: "0", + conclusion: "failure" } }, { @@ -96,7 +104,8 @@ module.exports = function(stats, options) { timestamp: "1970-01-01T00:01:40", // new Date(100001).toISOString().slice(0, -5) tests: "1", failures: "0", - time: "400.005" + time: "400.005", + retries: "0" } }, { @@ -104,7 +113,9 @@ module.exports = function(stats, options) { _attr: { name: "Another suite! works", classname: "works", - time: "400.004" + time: "400.004", + retries: "0", + conclusion: "success" } } } @@ -148,7 +159,8 @@ module.exports = function(stats, options) { tests: "1", failures: "0", skipped: "1", - time: "0.000" + time: "0.000", + retries: "0" } }, { @@ -157,7 +169,9 @@ module.exports = function(stats, options) { _attr: { name: "Pending suite! pending", classname: "pending", - time: "0.000" + time: "0.000", + retries: "0", + conclusion: "success" } }, { diff --git a/test/resources/JUnit.xsd b/test/resources/JUnit.xsd index 44b841f..d990c06 100644 --- a/test/resources/JUnit.xsd +++ b/test/resources/JUnit.xsd @@ -129,6 +129,16 @@ Permission to waive conditions of this license may be requested from Windy Road Time taken (in seconds) to execute the test + + + Number of times the testcase was re-run. + + + + + Conclusion of the testcase: success or failure + + @@ -192,6 +202,11 @@ Permission to waive conditions of this license may be requested from Windy Road The total number of tests in the suite that errorrd. An errored test is one that had an unanticipated problem. e.g., an unchecked throwable; or a problem with the implementation of the test. + + + The total number of tests in the suite that were rerun. + + Time taken (in seconds) to execute the tests in the suite diff --git a/test/resources/jenkins-junit.xsd b/test/resources/jenkins-junit.xsd index e8228ac..f89d4ed 100644 --- a/test/resources/jenkins-junit.xsd +++ b/test/resources/jenkins-junit.xsd @@ -75,6 +75,8 @@ + + @@ -90,6 +92,7 @@ +