playwright-trx-reporter
Version:
TRX reporter for playwright
126 lines (125 loc) • 5.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestRunBuilder = exports.NAME_SPLITTER = exports.allLoadedResults = exports.resultsNotInAList = exports.ALL_LOADED_RESULTS_ID = exports.RESULT_NOT_IN_A_LIST_ID = void 0;
const trxModel_1 = require("./trxModel");
const assert_1 = require("./assert");
// Magic number
// Copied from an existing trx file
exports.RESULT_NOT_IN_A_LIST_ID = '8c84fa94-04c1-424b-9868-57a2d4851a1d';
exports.ALL_LOADED_RESULTS_ID = '19431567-8539-422a-85d7-44ee4e166bda';
exports.resultsNotInAList = new trxModel_1.TestListType({ $id: exports.RESULT_NOT_IN_A_LIST_ID, $name: 'Results Not in a List' });
exports.allLoadedResults = new trxModel_1.TestListType({ $id: exports.ALL_LOADED_RESULTS_ID, $name: 'All Loaded Results' });
exports.NAME_SPLITTER = ' > ';
function createResultSummaryByCounters(counter) {
const $outcome = counter.$executed === counter.$passed ? 'Completed' : 'Failed';
return new trxModel_1.ResultSummary({
$outcome,
Counters: counter,
});
}
/**
* Create a whole trx object, which could be serilized by {@link serialize2Xml}
*/
class TestRunBuilder {
constructor(options) {
this._isBuilt = false;
const { id, name, runUser, endTime, startTime, } = options;
this._testRun = new trxModel_1.TestRunType({ $id: id, $name: name, $runUser: runUser });
this._Counters = new trxModel_1.CountersType();
this._Results = new trxModel_1.ResultsType({ UnitTestResult: [] });
this._TestDefinitions = new trxModel_1.TestDefinitionType({ UnitTest: [] });
this._TestEntries = new trxModel_1.TestEntriesType({ TestEntry: [] });
this._TestLists = {
TestList: [
exports.resultsNotInAList,
exports.allLoadedResults,
],
};
this._Times = new trxModel_1.Times({
$creation: startTime,
$finish: endTime,
});
}
// in which condition should we add other test type?
addTestResult(testResult, options) {
var _a, _b, _c, _d, _e, _f;
const { testDefinitionAdditionalInfo } = options;
const { owner, priority, fileLocation, workItemIds, className, } = testDefinitionAdditionalInfo;
const executionId = testResult.$executionId;
// add test definition
const unitTest = new trxModel_1.UnitTestType({
$id: testResult.$testId,
$name: [className, testResult.$testName].join(exports.NAME_SPLITTER),
$priority: priority,
$storage: fileLocation,
Owners: owner ? {
Owner: [{ $name: owner }],
} : undefined,
Execution: executionId ? { $id: executionId } : undefined,
WorkItemIDs: workItemIds
? { WorkItem: workItemIds.map((item) => ({ $id: item })) }
: undefined,
TestMethod: {
$className: className,
$codeBase: fileLocation,
$name: testResult.$testName,
},
});
// One test might be run more than once due to flaky.
// Only add the definition once.
if ((_b = (_a = this._TestDefinitions) === null || _a === void 0 ? void 0 : _a.UnitTest) === null || _b === void 0 ? void 0 : _b.every(((t) => t.$id !== unitTest.$id))) {
(_d = (_c = this._TestDefinitions) === null || _c === void 0 ? void 0 : _c.UnitTest) === null || _d === void 0 ? void 0 : _d.push(unitTest);
}
// add test entry
const testEntry = new trxModel_1.TestEntryType({
$executionId: testResult.$executionId,
$testId: testResult.$testId,
$testListId: testResult.$testListId,
});
(_e = this._TestEntries.TestEntry) === null || _e === void 0 ? void 0 : _e.push(testEntry);
// add test result
(_f = this._Results.UnitTestResult) === null || _f === void 0 ? void 0 : _f.push(testResult);
// counter
this.count(testResult.$outcome);
}
count(outCome) {
this._Counters.$total += 1;
switch (outCome) {
case trxModel_1.TestOutcome.Aborted:
this._Counters.$aborted += 1;
break;
case trxModel_1.TestOutcome.Inconclusive:
this._Counters.$executed += 1;
this._Counters.$inconclusive += 1;
break;
case trxModel_1.TestOutcome.Timeout: // Intentional, MSTest(C#) handles `timeout` as `failed`.
case trxModel_1.TestOutcome.Failed:
this._Counters.$executed += 1;
this._Counters.$failed += 1;
break;
case trxModel_1.TestOutcome.Passed:
this._Counters.$executed += 1;
this._Counters.$passed += 1;
break;
case trxModel_1.TestOutcome.NotExecuted:
this._Counters.$notExecuted += 1;
break;
default:
break;
}
}
build() {
if (this._isBuilt) {
(0, assert_1.assert)("'TestRunBuilder' should not call `build` twice.");
}
this._isBuilt = true;
this._testRun.ResultSummary = createResultSummaryByCounters(this._Counters);
this._testRun.Results = this._Results;
this._testRun.TestDefinitions = this._TestDefinitions;
this._testRun.TestEntries = this._TestEntries;
this._testRun.TestLists = this._TestLists;
this._testRun.Times = this._Times;
return this._testRun;
}
}
exports.TestRunBuilder = TestRunBuilder;