jest-trx-results-processor
Version:
Jest results processor for exporting into TRX files for Visual Studio
206 lines (205 loc) • 9.39 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTrx = void 0;
var path = __importStar(require("path"));
var uuid_1 = require("uuid");
var xmlbuilder_1 = require("xmlbuilder");
var constants_1 = require("./constants");
var utils_1 = require("./utils");
var renderTestRun = function (builder, testRunResult, computerName, userName) {
builder
.att("id", (0, uuid_1.v4)())
.att("name", "".concat(userName, "@").concat(computerName, " ").concat(new Date(testRunResult.startTime).toISOString()))
.att("runUser", userName)
.att("xmlns", "http://microsoft.com/schemas/VisualStudio/TeamTest/2010");
};
var renderTestSettings = function (parentNode) {
parentNode
.ele("TestSettings")
.att("name", "Jest test run")
.att("id", (0, uuid_1.v4)());
};
var renderTimes = function (parentNode, testRunResult) {
var startTime = new Date(testRunResult.startTime).toISOString();
var totalDuration = testRunResult.testResults.reduce(function (acc, tr) { return acc + tr.perfStats.runtime; }, 0);
var finishTime = new Date(testRunResult.startTime + totalDuration).toISOString();
parentNode
.ele("Times")
.att("creation", startTime)
.att("queuing", startTime)
.att("start", startTime)
.att("finish", finishTime);
};
var renderResultSummary = function (parentNode, testRunResult) {
// workaround for https://github.com/facebook/jest/issues/6924
var anyTestFailures = !(testRunResult.numFailedTests === 0 &&
testRunResult.numRuntimeErrorTestSuites === 0);
var isSuccess = !(anyTestFailures || testRunResult.snapshot.failure);
var summary = parentNode
.ele("ResultSummary")
.att("outcome", isSuccess ? "Passed" : "Failed");
summary
.ele("Counters")
.att("total", testRunResult.numTotalTests + testRunResult.numRuntimeErrorTestSuites)
.att("executed", testRunResult.numTotalTests - testRunResult.numPendingTests)
.att("passed", testRunResult.numPassedTests)
.att("failed", testRunResult.numFailedTests)
.att("error", testRunResult.numRuntimeErrorTestSuites);
};
var renderTestLists = function (parentNode) {
var testLists = parentNode.ele("TestLists");
testLists
.ele("TestList")
.att("name", "Results Not in a List")
.att("id", constants_1.testListNotInListId);
testLists
.ele("TestList")
.att("name", "All Loaded Results")
.att("id", constants_1.testListAllLoadedResultsId);
};
var renderTestSuiteResult = function (testSuiteResult, testDefinitionsNode, testEntriesNode, resultsNode, computerName, postProcessTestResult) {
var _a;
if (testSuiteResult.testResults && testSuiteResult.testResults.length) {
var runningDuration_1 = 0;
testSuiteResult.testResults.forEach(function (testResult) {
var testId = (0, uuid_1.v4)();
var executionId = (0, uuid_1.v4)();
var fullTestName = (0, utils_1.getFullTestName)(testResult);
var filepath = path.relative("./", testSuiteResult.testFilePath);
var duration = testResult.duration || 0;
// UnitTest
var unitTest = testDefinitionsNode
.ele("UnitTest")
.att("name", fullTestName)
.att("id", testId)
.att("storage", filepath);
unitTest.ele("Execution").att("id", executionId);
unitTest
.ele("TestMethod")
.att("codeBase", filepath)
.att("name", fullTestName)
.att("className", (0, utils_1.getTestClassName)(testResult));
// TestEntry
testEntriesNode
.ele("TestEntry")
.att("testId", testId)
.att("executionId", executionId)
.att("testListId", constants_1.testListNotInListId);
// UnitTestResult
var result = resultsNode
.ele("UnitTestResult")
.att("testId", testId)
.att("executionId", executionId)
.att("testName", fullTestName)
.att("computerName", computerName)
.att("duration", (0, utils_1.formatDuration)(duration))
.att("startTime", new Date(testSuiteResult.perfStats.start).toISOString())
.att("endTime", new Date(testSuiteResult.perfStats.start + runningDuration_1).toISOString())
.att("testType", constants_1.testType)
.att("outcome", constants_1.testOutcomeTable[testResult.status])
.att("testListId", constants_1.testListNotInListId);
runningDuration_1 += duration;
if (testResult.status === "failed") {
result
.ele("Output")
.ele("ErrorInfo")
.ele("Message", testResult.failureMessages.join("\n"));
}
// Perform any post processing for this test result.
if (postProcessTestResult) {
postProcessTestResult.forEach(function (postProcess) {
return postProcess(testSuiteResult, testResult, result);
});
}
});
}
if ((_a = testSuiteResult.testExecError) === null || _a === void 0 ? void 0 : _a.stack) {
// For suites that failed to run, we will generate a test result that documents the failure.
// This occurs when there is a failure compiling/loading the suite or an assertion in a before/after hook fails,
// not when a test in the suite fails.
var testId = (0, uuid_1.v4)();
var executionId = (0, uuid_1.v4)();
var fullTestName = path.basename(testSuiteResult.testFilePath);
var time = new Date().toISOString();
var filepath = path.relative("./", testSuiteResult.testFilePath);
// Failed TestSuite
var unitTest = testDefinitionsNode
.ele("UnitTest")
.att("name", fullTestName)
.att("id", testId)
.att("storage", filepath);
unitTest.ele("Execution").att("id", executionId);
unitTest
.ele("TestMethod")
.att("codeBase", filepath)
.att("name", fullTestName)
.att("className", fullTestName);
// TestEntry
testEntriesNode
.ele("TestEntry")
.att("testId", testId)
.att("executionId", executionId)
.att("testListId", constants_1.testListNotInListId);
// UnitTestResult
var result = resultsNode
.ele("UnitTestResult")
.att("testId", testId)
.att("executionId", executionId)
.att("testName", fullTestName)
.att("computerName", computerName)
.att("duration", "0")
.att("startTime", time)
.att("endTime", time)
.att("testType", constants_1.testType)
.att("outcome", constants_1.testOutcomeTable.failed)
.att("testListId", constants_1.testListNotInListId);
result
.ele("Output")
.ele("ErrorInfo")
.ele("Message", testSuiteResult.testExecError.stack);
}
};
var generateTrx = function (testRunResult, options) {
var _a = (0, utils_1.getEnvInfo)(options && options.defaultUserName), computerName = _a.computerName, userName = _a.userName;
var resultBuilder = (0, xmlbuilder_1.create)("TestRun", {
invalidCharReplacement: "",
version: "1.0",
encoding: "UTF-8",
});
renderTestRun(resultBuilder, testRunResult, computerName, userName);
renderTestSettings(resultBuilder);
renderTimes(resultBuilder, testRunResult);
renderResultSummary(resultBuilder, testRunResult);
var testDefinitions = resultBuilder.ele("TestDefinitions");
renderTestLists(resultBuilder);
var testEntries = resultBuilder.ele("TestEntries");
var results = resultBuilder.ele("Results");
testRunResult.testResults.forEach(function (testSuiteResult) {
return renderTestSuiteResult(testSuiteResult, testDefinitions, testEntries, results, computerName, options && options.postProcessTestResult);
});
return resultBuilder.end({ pretty: true });
};
exports.generateTrx = generateTrx;
;