@buddy-works/unit-tests
Version:
Universal test results collector for Jest, Jasmine, Mocha, Cypress, Playwright, and Vitest that sends results to Buddy Works API in real-time
185 lines (184 loc) • 9.2 kB
JavaScript
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a, _TestResultMapper_toXml, _TestResultMapper_stripAnsiCodes, _TestResultMapper_getStatusFromTestResult;
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../core/types");
const logger_1 = __importDefault(require("../utils/logger"));
class TestResultMapper {
static mapJestResult(assertionResult, testResult, relativeFilePath) {
const status = __classPrivateFieldGet(this, _a, "m", _TestResultMapper_getStatusFromTestResult).call(this, assertionResult.status, {
passed: types_1.BUDDY_UNIT_TEST_STATUS.PASSED,
failed: types_1.BUDDY_UNIT_TEST_STATUS.FAILED,
skipped: types_1.BUDDY_UNIT_TEST_STATUS.SKIPPED,
pending: types_1.BUDDY_UNIT_TEST_STATUS.SKIPPED,
todo: types_1.BUDDY_UNIT_TEST_STATUS.SKIPPED,
disabled: types_1.BUDDY_UNIT_TEST_STATUS.SKIPPED,
focused: types_1.BUDDY_UNIT_TEST_STATUS.SKIPPED,
});
const testGroupName = relativeFilePath || testResult.testFilePath;
const nameParts = [];
if (assertionResult.ancestorTitles.length > 0) {
nameParts.push(...assertionResult.ancestorTitles);
}
nameParts.push(assertionResult.title);
const testName = nameParts.join(' > ');
const dataObject = {
errorMessage: assertionResult.failureMessages.length > 0 ? assertionResult.failureMessages.join('\n') : '',
errorStackTrace: assertionResult.failureMessages.length > 0 ? assertionResult.failureMessages.join('\n') : '',
messages: assertionResult.ancestorTitles.join(' > ') || '',
};
return {
name: testName,
classname: testGroupName,
test_group_name: testGroupName,
status,
time: assertionResult.duration ? assertionResult.duration / 1000 : 0,
data: __classPrivateFieldGet(this, _a, "m", _TestResultMapper_toXml).call(this, dataObject),
};
}
static mapJasmineResult(result, relativeFilePath) {
const status = __classPrivateFieldGet(this, _a, "m", _TestResultMapper_getStatusFromTestResult).call(this, result.status, {
passed: types_1.BUDDY_UNIT_TEST_STATUS.PASSED,
failed: types_1.BUDDY_UNIT_TEST_STATUS.FAILED,
pending: types_1.BUDDY_UNIT_TEST_STATUS.SKIPPED,
});
const testGroupName = relativeFilePath || result.filename || 'Unknown Test Group';
const testName = result.fullName || result.description;
const dataObject = {
errorMessage: result.failedExpectations.map((exp) => exp.message).join('\n') || '',
errorStackTrace: result.failedExpectations.map((exp) => exp.stack).join('\n') || '',
messages: result.fullName || '',
};
return {
name: testName,
classname: testGroupName,
test_group_name: testGroupName,
status: status,
time: (result.duration ?? 0) / 1000 || 0,
data: __classPrivateFieldGet(this, _a, "m", _TestResultMapper_toXml).call(this, dataObject),
};
}
static mapMochaResult(test, relativeFilePath) {
const status = __classPrivateFieldGet(this, _a, "m", _TestResultMapper_getStatusFromTestResult).call(this, test.state, {
passed: types_1.BUDDY_UNIT_TEST_STATUS.PASSED,
failed: types_1.BUDDY_UNIT_TEST_STATUS.FAILED,
pending: types_1.BUDDY_UNIT_TEST_STATUS.SKIPPED,
});
const testGroupName = relativeFilePath || test.file || 'Unknown Test Group';
const testName = test.fullTitle() || test.title;
const dataObject = {
errorMessage: test.err ? test.err.message : '',
errorStackTrace: test.err ? test.err.stack : '',
messages: test.fullTitle() || '',
};
return {
name: testName,
classname: testGroupName,
test_group_name: testGroupName,
status: status,
time: test.duration ? test.duration / 1000 : 0,
data: __classPrivateFieldGet(this, _a, "m", _TestResultMapper_toXml).call(this, dataObject),
};
}
static mapPlaywrightResult(test, result, relativeFilePath) {
const status = __classPrivateFieldGet(this, _a, "m", _TestResultMapper_getStatusFromTestResult).call(this, result.status, {
passed: types_1.BUDDY_UNIT_TEST_STATUS.PASSED,
failed: types_1.BUDDY_UNIT_TEST_STATUS.FAILED,
skipped: types_1.BUDDY_UNIT_TEST_STATUS.SKIPPED,
timedOut: types_1.BUDDY_UNIT_TEST_STATUS.FAILED,
});
const testGroupName = relativeFilePath || test.location.file;
const titles = [];
let parent = test.parent;
while (parent) {
if (parent.title &&
parent.title !== testGroupName &&
parent.title !== test.location.file) {
titles.unshift(parent.title);
}
parent = parent.parent;
}
const nameParts = [];
if (titles.length > 0) {
nameParts.push(...titles);
}
nameParts.push(test.title);
const testName = nameParts.join(' > ');
const dataObject = {
errorMessage: result.error ? __classPrivateFieldGet(this, _a, "m", _TestResultMapper_stripAnsiCodes).call(this, result.error.message || '') : '',
errorStackTrace: result.error ? __classPrivateFieldGet(this, _a, "m", _TestResultMapper_stripAnsiCodes).call(this, result.error.stack || '') : '',
messages: test.location.file || '',
};
return {
name: testName,
classname: testGroupName,
test_group_name: testGroupName,
status: status,
time: result.duration ? result.duration / 1000 : 0,
data: __classPrivateFieldGet(this, _a, "m", _TestResultMapper_toXml).call(this, dataObject),
};
}
static mapVitestResult(taskId, taskResult, task, relativeFilePath) {
const status = __classPrivateFieldGet(this, _a, "m", _TestResultMapper_getStatusFromTestResult).call(this, taskResult.state, {
pass: types_1.BUDDY_UNIT_TEST_STATUS.PASSED,
fail: types_1.BUDDY_UNIT_TEST_STATUS.FAILED,
skip: types_1.BUDDY_UNIT_TEST_STATUS.SKIPPED,
});
const testGroupName = relativeFilePath || 'Unknown Test Group';
let testName = 'Unknown Test';
if (task) {
const nameParts = [];
if (task.suite?.name) {
nameParts.push(task.suite.name);
}
if (task.name) {
nameParts.push(task.name);
}
else if (taskId) {
nameParts.push(taskId);
}
testName = nameParts.join(' > ');
}
else if (taskId) {
testName = taskId;
}
const dataObject = {
errorMessage: (taskResult.errors?.length ?? 0) > 0 ? taskResult.errors?.map((error) => error.message).join('\n') : '',
errorStackTrace: (taskResult.errors?.length ?? 0) > 0 ? taskResult.errors?.map((error) => error.stack).join('\n') : '',
messages: task?.suite?.name || '',
};
return {
name: testName,
classname: testGroupName,
test_group_name: testGroupName,
status,
time: taskResult.duration ? taskResult.duration / 1000 : 0,
data: __classPrivateFieldGet(this, _a, "m", _TestResultMapper_toXml).call(this, dataObject),
};
}
}
_a = TestResultMapper, _TestResultMapper_toXml = function _TestResultMapper_toXml(object) {
let xml = '<data>';
for (const [key, value] of Object.entries(object)) {
xml += value ? `<${key}><![CDATA[${value}]]></${key}>` : `<${key}></${key}>`;
}
xml += '</data>';
return xml;
}, _TestResultMapper_stripAnsiCodes = function _TestResultMapper_stripAnsiCodes(text) {
return text.replace(/\u001B\[[0-9;]*[mGKHF]/g, '');
}, _TestResultMapper_getStatusFromTestResult = function _TestResultMapper_getStatusFromTestResult(testResult, statusMap) {
return (statusMap[testResult] ??
(() => {
logger_1.default.debug(`Unknown test result status: ${String(testResult)}. Defaulting to ${types_1.BUDDY_UNIT_TEST_STATUS.ERROR}.`);
return types_1.BUDDY_UNIT_TEST_STATUS.ERROR;
})());
};
TestResultMapper.displayName = 'TestResultMapper';
exports.default = TestResultMapper;