UNPKG

cypress-xray-plugin

Version:

A Cypress plugin for uploading test results to Xray (test management for Jira)

127 lines (123 loc) 5.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImportExecutionCypressCommand = void 0; const xray_client_cloud_1 = require("../../../../client/xray/xray-client-cloud"); const xray_client_server_1 = require("../../../../client/xray/xray-client-server"); const dedent_1 = require("../../../../util/dedent"); const logging_1 = require("../../../../util/logging"); const string_1 = require("../../../../util/string"); const command_1 = require("../../../command"); class ImportExecutionCypressCommand extends command_1.Command { constructor(parameters, logger, execution) { super(parameters, logger); this.execution = execution; } async computeResult() { const [results, info] = await this.execution.compute(); let testExecutionIssueKey; if (this.parameters.splitUpload) { const evidencyByTestIssue = new Map(); if (results.tests) { for (const test of results.tests) { if (test.testKey && test.evidence) { evidencyByTestIssue.set(test.testKey, test.evidence); delete test.evidence; } } } testExecutionIssueKey = await this.parameters.xrayClient.importExecutionMultipart(results, info); const entries = [...evidencyByTestIssue.entries()]; const uploadCallbacks = entries.map(async ([issueKey, evidences]) => { try { await this.uploadTestEvidences(issueKey, testExecutionIssueKey, evidences); } catch (error) { logging_1.LOG.message("warning", (0, dedent_1.dedent)(` Failed to attach evidences of test ${issueKey} to test execution ${testExecutionIssueKey}: ${(0, string_1.unknownToString)(error)} `)); } }); await Promise.all(uploadCallbacks); } else { testExecutionIssueKey = await this.parameters.xrayClient.importExecutionMultipart(results, info); } await this.parameters.emitter.emit("upload:cypress", { info, results, testExecutionIssueKey, }); return testExecutionIssueKey; } async uploadTestEvidences(issueKey, testExecIssueKey, evidences) { let uploadCallbacks = []; if (this.parameters.xrayClient instanceof xray_client_server_1.ServerClient) { const serverClient = this.parameters.xrayClient; const testRun = await serverClient.getTestRun({ testExecIssueKey: testExecIssueKey, testIssueKey: issueKey, }); uploadCallbacks = evidences.map((evidence) => () => this.uploadEvidenceServer(serverClient, { evidence, issueKey, testExecIssueKey, testRunId: testRun.id, })); } else if (this.parameters.xrayClient instanceof xray_client_cloud_1.XrayClientCloud) { const cloudClient = this.parameters.xrayClient; const testRuns = await cloudClient.getTestRunResults({ testExecIssueIds: [testExecIssueKey], testIssueIds: [issueKey], }); if (testRuns.length !== 1) { throw new Error(`Failed to get test run for test execution ${testExecIssueKey} and test ${issueKey}`); } if (!testRuns[0].id) { throw new Error("Test run does not have an ID"); } const id = testRuns[0].id; uploadCallbacks = evidences.map((evidence) => () => this.uploadEvidenceCloud(cloudClient, { evidence, issueKey, testExecIssueKey, testRunId: id, })); } if (this.parameters.splitUpload === "sequential") { for (const uploadCallback of uploadCallbacks) { await uploadCallback(); } } else { await Promise.all(uploadCallbacks.map((callback) => callback())); } } async uploadEvidenceServer(serverClient, testRunConfig) { try { await serverClient.addEvidence(testRunConfig.testRunId, testRunConfig.evidence); } catch (error) { logging_1.LOG.message("warning", (0, dedent_1.dedent)(` Failed to attach evidence of test ${testRunConfig.issueKey} to test execution ${testRunConfig.testExecIssueKey}: ${(0, string_1.unknownToString)(error)} `)); } } async uploadEvidenceCloud(cloudClient, testRunConfig) { try { await cloudClient.addEvidenceToTestRun({ evidence: [testRunConfig.evidence], id: testRunConfig.testRunId, }); } catch (error) { logging_1.LOG.message("warning", (0, dedent_1.dedent)(` Failed to attach evidence of test ${testRunConfig.issueKey} to test execution ${testRunConfig.testExecIssueKey}: ${(0, string_1.unknownToString)(error)} `)); } } } exports.ImportExecutionCypressCommand = ImportExecutionCypressCommand;