UNPKG

playwright-teamcity-reporter

Version:
189 lines 9.12 kB
"use strict"; 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; }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; 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 _TeamcityReporter_instances, _TeamcityReporter_testMetadataArtifacts, _TeamcityReporter_flowIds, _TeamcityReporter_logAttachment, _TeamcityReporter_writeTestFlow, _TeamcityReporter_getFlowId; Object.defineProperty(exports, "__esModule", { value: true }); const crypto_1 = require("crypto"); const path = __importStar(require("path")); const errors_1 = require("./errors"); const utils_1 = require("./utils"); // https://www.jetbrains.com/help/teamcity/service-messages.html class TeamcityReporter { constructor(configuration) { this.configuration = configuration; _TeamcityReporter_instances.add(this); _TeamcityReporter_testMetadataArtifacts.set(this, void 0); _TeamcityReporter_flowIds.set(this, new Map()); __classPrivateFieldSet(this, _TeamcityReporter_testMetadataArtifacts, configuration?.testMetadataArtifacts ?? process.env.TEAMCITY_ARTIFACTS_PW_RESULT ?? "test-results", "f"); } printsToStdio() { return true; } onBegin(config) { if (this.configuration?.logConfig) { (0, utils_1.writeServiceMessage)("message", { text: (0, utils_1.stringify)(config) }); } // https://www.jetbrains.com/help/teamcity/service-messages.html#Enabling+Test+Retry if (config.projects.some(project => project.retries > 0)) { (0, utils_1.writeServiceMessage)("testRetrySupport", { enabled: "true" }); } } onTestBegin(test) { __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_writeTestFlow).call(this, "testStarted", test); } onStdOut(chunk, test) { if (test) { __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_writeTestFlow).call(this, "testStdOut", test, { out: chunk.toString() }); } else { console.log(chunk); } } onStdErr(chunk, test) { if (test) { __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_writeTestFlow).call(this, "testStdErr", test, { out: chunk.toString() }); } else { console.error(chunk); } } onTestEnd(test, result) { switch (result.status) { case "skipped": __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_writeTestFlow).call(this, "testIgnored", test, { message: "skipped", }); break; case "timedOut": __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_writeTestFlow).call(this, "testFailed", test, { message: `${result.error?.message ?? ""}`, details: result.errors.map((e) => e.message).join("\n"), }); break; case "failed": __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_writeTestFlow).call(this, "testFailed", test, { message: `${result.error?.message ?? ""}`, details: `${result.error?.stack ?? ""}`, }); break; case "interrupted": __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_writeTestFlow).call(this, "testFailed", test, { message: "Test interrupted", }); break; case "passed": break; default: throw new errors_1.NotImplementedError(`${result?.status} isn't supported`); } for (const attachment of result.attachments || []) { __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_logAttachment).call(this, test, attachment); } __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_writeTestFlow).call(this, "testFinished", test, { duration: `${result.duration}` }); } onError(error) { console.error(error); } onEnd(result) { console.info(`Finished the run: ${result.status}`); } } _TeamcityReporter_testMetadataArtifacts = new WeakMap(), _TeamcityReporter_flowIds = new WeakMap(), _TeamcityReporter_instances = new WeakSet(), _TeamcityReporter_logAttachment = function _TeamcityReporter_logAttachment(test, attachment) { // https://www.jetbrains.com/help/teamcity/service-messages.html#Reporting+Additional+Test+Data // "test-results" should be a part of the artifacts directory let value = ""; if (attachment.path !== undefined) { if (attachment.path.indexOf("test-results") === -1) return; const artifact = __classPrivateFieldGet(this, _TeamcityReporter_testMetadataArtifacts, "f"); value = attachment.path; value = value.split(path.sep).join(path.posix.sep); value = value.slice(value.indexOf("test-results") + 13); value = `${artifact}${artifact.endsWith(".zip") ? "!" : ""}/${value}`; } else if (attachment.body !== undefined) { value = attachment.body.toString("base64"); } let type; switch (attachment.contentType) { case "image/png": type = "image"; break; case "video/mp4": case "video/webm": type = "video"; break; case "application/zip": type = "artifact"; break; case "application/json": default: type = "text"; } (0, utils_1.writeServiceMessage)("testMetadata", { type, testName: (0, utils_1.getTestName)(test), name: attachment.name, value, flowId: __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_getFlowId).call(this, test), }); }, _TeamcityReporter_writeTestFlow = function _TeamcityReporter_writeTestFlow(messageName, test, parts = {}) { (0, utils_1.writeServiceMessage)(messageName, { name: (0, utils_1.getTestName)(test), ...parts, flowId: __classPrivateFieldGet(this, _TeamcityReporter_instances, "m", _TeamcityReporter_getFlowId).call(this, test), }); }, _TeamcityReporter_getFlowId = function _TeamcityReporter_getFlowId(test) { let flowId = __classPrivateFieldGet(this, _TeamcityReporter_flowIds, "f").get(test); if (flowId === undefined) { flowId = (0, crypto_1.randomUUID)(); __classPrivateFieldGet(this, _TeamcityReporter_flowIds, "f").set(test, flowId); } return flowId; }; exports.default = TeamcityReporter; //# sourceMappingURL=teamcity.reporter.js.map