@salesforce/apex-node
Version:
Salesforce JS library for Apex
108 lines • 4.83 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JUnitReporter = void 0;
const utils_1 = require("../utils");
const core_1 = require("@salesforce/core");
const narrowing_1 = require("../narrowing");
// cli currently has spaces in multiples of four for junit format
const tab = ' ';
const timeProperties = [
'testExecutionTimeInMs',
'testTotalTimeInMs',
'commandTimeInMs'
];
// properties not in cli junit spec
const skippedProperties = ['skipRate', 'totalLines', 'linesCovered'];
class JUnitReporter {
format(testResult) {
utils_1.HeapMonitor.getInstance().checkHeapSize('JUnitReporter.format');
try {
const { summary, tests } = testResult;
let output = `<?xml version="1.0" encoding="UTF-8"?>\n`;
output += `<testsuites>\n`;
output += `${tab}<testsuite name="force.apex" `;
output += `timestamp="${summary.testStartTime}" `;
output += `hostname="${summary.hostname}" `;
output += `tests="${summary.testsRan}" `;
output += `failures="${summary.failing}" `;
output += `errors="0" `;
output += `time="${(0, utils_1.msToSecond)(summary.testExecutionTimeInMs)}">\n`;
output += this.buildProperties(testResult);
output += this.buildTestCases(tests);
output += `${tab}</testsuite>\n`;
output += `</testsuites>\n`;
return output;
}
finally {
utils_1.HeapMonitor.getInstance().checkHeapSize('JUnitReporter.format');
}
}
buildProperties(testResult) {
let junitProperties = `${tab}${tab}<properties>\n`;
Object.entries(testResult.summary).forEach(([key, value]) => {
if ((0, narrowing_1.isEmpty)(value) || skippedProperties.includes(key)) {
return;
}
if (timeProperties.includes(key)) {
value = `${(0, utils_1.msToSecond)(value)} s`;
key = key.replace('InMs', '');
}
if (key === 'outcome' && value === 'Passed') {
value = 'Successful';
}
if (key === 'testStartTime') {
value = (0, utils_1.formatStartTime)(value);
}
junitProperties += `${tab}${tab}${tab}<property name="${key}" value="${value}"/>\n`;
});
junitProperties += `${tab}${tab}</properties>\n`;
return junitProperties;
}
buildTestCases(tests) {
let junitTests = '';
for (const testCase of tests) {
const methodName = this.xmlEscape(testCase.methodName);
junitTests += `${tab}${tab}<testcase name="${methodName}" classname="${testCase.apexClass.fullName}" time="${(0, utils_1.msToSecond)(testCase.runTime)}">\n`;
if (testCase.outcome === "Fail" /* ApexTestResultOutcome.Fail */ ||
testCase.outcome === "CompileFail" /* ApexTestResultOutcome.CompileFail */) {
let message = (0, narrowing_1.isEmpty)(testCase.message) ? '' : testCase.message;
message = this.xmlEscape(message);
junitTests += `${tab}${tab}${tab}<failure message="${message}">`;
if (testCase.stackTrace) {
junitTests += `<![CDATA[${testCase.stackTrace}]]>`;
}
junitTests += `</failure>\n`;
}
junitTests += `${tab}${tab}</testcase>\n`;
}
return junitTests;
}
xmlEscape(value) {
return value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
}
exports.JUnitReporter = JUnitReporter;
__decorate([
(0, utils_1.elapsedTime)()
], JUnitReporter.prototype, "format", null);
__decorate([
(0, utils_1.elapsedTime)()
], JUnitReporter.prototype, "buildProperties", null);
__decorate([
(0, utils_1.elapsedTime)()
], JUnitReporter.prototype, "buildTestCases", null);
__decorate([
(0, utils_1.elapsedTime)('elapsedTime', core_1.LoggerLevel.TRACE)
], JUnitReporter.prototype, "xmlEscape", null);
//# sourceMappingURL=junitReporter.js.map