junit-xml
Version:
JUnit XML report builder with TypeScript support
99 lines (98 loc) • 4.01 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
*
* @param report A report in the default schema, as supported by GitLab
*/
function getXmlObject(report) {
var testSuites = report.suites.map(attachTestSuiteMetadata);
var xmlObject = {
testsuites: testSuites
.filter(function (testSuite) { return testSuite.tests > 0; })
.map(function (testSuite, index) { return ({ testsuite: getTestSuiteXmlObject(testSuite, index) }); }),
};
return xmlObject;
}
exports.getXmlObject = getXmlObject;
function getTestSuiteXmlObject(testSuite, index) {
var systemOuts = testSuite.testCases
.filter(function (testCase) { return Array.isArray(testCase.systemOut) && testCase.systemOut.length > 0; })
.map(function (outTestCase) { return outTestCase.systemOut.join('\n'); })
.join('\n');
var systemErrs = testSuite.testCases
.filter(function (testCase) { return Array.isArray(testCase.systemErr) && testCase.systemErr.length > 0; })
.map(function (errTestCase) { return errTestCase.systemErr.join('\n'); })
.join('\n');
var testSuiteXmlObject = testSuite.testCases
.map(function (testCase) { return ({ testcase: getTestCaseXmlObject(testCase) }); });
testSuiteXmlObject.push({ 'system-out': systemOuts });
testSuiteXmlObject.push({ 'system-err': systemErrs });
testSuiteXmlObject.unshift({ properties: [] });
var testsuiteAttributes = {
package: testSuite.name || "Test suite #" + index,
id: index.toString(),
name: testSuite.name || "Test suite #" + index,
timestamp: mangleIsoDate((testSuite.timestamp || new Date(0)).toISOString()),
hostname: testSuite.hostname || 'localhost',
tests: testSuite.tests.toString(),
errors: testSuite.errors.toString(),
failures: testSuite.failures.toString(),
time: (testSuite.time || 0).toString(),
};
testSuiteXmlObject.push({ _attr: testsuiteAttributes });
return testSuiteXmlObject;
}
function getTestCaseXmlObject(testCase) {
var testCaseXmlObject = [];
var testcaseAttributes = {
name: testCase.name,
classname: testCase.classname || testCase.name,
time: (testCase.time || 0).toString(),
};
testCaseXmlObject.push({ _attr: testcaseAttributes });
if (Array.isArray(testCase.errors)) {
testCaseXmlObject.push({ error: formatMessages(testCase.errors) });
}
if (Array.isArray(testCase.failures)) {
testCaseXmlObject.push({ failure: formatMessages(testCase.failures) });
}
return testCaseXmlObject;
}
;
function attachTestSuiteMetadata(testSuite) {
return __assign({}, testSuite, { tests: testSuite.testCases.length, failures: testSuite.testCases.filter(isFailing).length, errors: testSuite.testCases.filter(hasErrors).length, skipped: testSuite.testCases.filter(isSkipped).length });
}
function isSkipped(testCase) {
return testCase.skipped === true;
}
function isFailing(testCase) {
return Array.isArray(testCase.failures) && testCase.failures.length > 0;
}
function hasErrors(testCase) {
return Array.isArray(testCase.errors) && testCase.errors.length > 0;
}
function formatMessages(messages) {
return {
_attr: {
message: messages.map(function (message) { return message.message; }).join(', '),
type: 'Unkown',
},
};
}
/**
* The schema uses an ISO8601 date regex that does not include the timezone; this function strips it.
*/
function mangleIsoDate(isoDate) {
return isoDate.substring(0, isoDate.length - 5);
}
;