UNPKG

@zebrunner/javascript-agent-mocha

Version:
279 lines (222 loc) 8.64 kB
const ConfigResolver = require('./config-resolver'); const ZebrunnerApiClient = require('./zebrunner-api-client'); const Storage = require('./storage'); const { LOCALE, testStatuses } = require('./constants'); const { isBlankString } = require('./utils'); class ZebrunnerReporter { constructor(config) { this.reporterConfig = config.reporterConfig; this.configResolver = new ConfigResolver(this.reporterConfig); this.zebrunnerClient = new ZebrunnerApiClient(this.reporterConfig); this.storage = new Storage(); } onLaunchStart = async () => { this.storage.runId = this.zebrunnerClient.startLaunch(); // await this.#saveTcmConfigs(); // TODO: to check TCM integration await this.#saveTestRunLabels(); await this.#saveTestRunArtifactReferences(); }; #saveTcmConfigs = async () => { const runId = await this.storage.runId; if (runId) { // TODO: to check TCM integration and saving configs before test cases await this.zebrunnerClient.saveTcmConfigs(runId); // this.storage.addArtifactFinishPromise(updateTcmConfigsPromise); } }; #saveTestRunLabels = async () => { const runId = await this.storage.runId; if (runId) { const labels = this.configResolver.getReportingRunLabels(); const locale = this.configResolver.getReportingRunLocale(); if (locale) { labels.push({ key: LOCALE, value: locale, }); } const attachLabelsPromise = this.zebrunnerClient.attachTestRunLabels(runId, labels); this.storage.addArtifactFinishPromise(attachLabelsPromise); } }; #saveTestRunArtifactReferences = async () => { const runId = await this.storage.runId; if (runId) { const artifactReferences = this.configResolver.getReportingRunArtifactReferences(); const attachReferencesPromise = this.zebrunnerClient.attachTestRunArtifactReferences( runId, artifactReferences, ); this.storage.addArtifactFinishPromise(attachReferencesPromise); } }; onLaunchFinish = async () => { const runId = await this.storage.runId; if (runId) { await Promise.all(this.storage.getAllTestsStartPromises()); await Promise.all([ ...this.storage.getAllTestsFinishPromises(), ...this.storage.artifactsFinishPromises, ]); return this.zebrunnerClient.finishLaunch(runId); } }; onTestStart = async (test) => { const runId = await this.storage.runId; if (runId) { const testStartPromise = this.zebrunnerClient.startTest(runId, test); this.storage.setTestStartPromise(test.uniqueId, testStartPromise); } }; onTestFinish = async (test) => { const runId = await this.storage.runId; if (runId) { const testId = await this.storage.getTestId(test.uniqueId); if (testId) { const currentTest = this.storage.getTestByUniqueId(test.uniqueId); if (currentTest.revertPromise) { // test registration has been reverted, nothing to finish await currentTest.revertPromise; return; } if (currentTest.updatePromise) { // necessary to wait for test update promises if they exist before test finish await currentTest.updatePromise; } const testCases = this.storage.getTestTestCases(test.uniqueId); if (testCases.length !== 0) { // update results for TCM test cases const defaultTestCaseStatus = this.#getDefaultTestCaseStatus(test.status); const updatedTestCases = this.#setDefaultStatusIfActualNotProvided( testCases, defaultTestCaseStatus, ); const upsertTestCasesPromise = this.zebrunnerClient.upsertTestTestCases( runId, testId, updatedTestCases, ); this.storage.addArtifactFinishPromise(upsertTestCasesPromise); } const testFinishPromise = this.zebrunnerClient.finishTest(runId, testId, test); this.storage.setTestFinishPromise(test.uniqueId, testFinishPromise); } } }; /** Get default status for TCM test case if it is provided */ #getDefaultTestCaseStatus = (testStatus) => { if (testStatus === testStatuses.PASSED) { return this.configResolver.getReportingTcmTestCaseStatusOnPass(); } if (testStatus === testStatuses.FAILED) { return this.configResolver.getReportingTcmTestCaseStatusOnFail(); } }; /** Set default status for TCM test case if there is no actual provided */ // eslint-disable-next-line class-methods-use-this #setDefaultStatusIfActualNotProvided = (testCases, defaultTestCaseStatus) => { if (!isBlankString(defaultTestCaseStatus)) { return testCases.map((_testCase) => { const testCase = { ..._testCase }; if (!testCase.resultStatus) { testCase.resultStatus = defaultTestCaseStatus; } return testCase; }); } return testCases; }; // eslint-disable-next-line class-methods-use-this, no-unused-vars onTestFail(test) { // TODO: necessary to send an error for failed beforeEach hooks } onTestPending = (test) => { const currentTest = this.storage.getTestByUniqueId(test.uniqueId); if (!currentTest) { this.onTestStart(test); } }; attachTestRunLabels = async (labels) => { const runId = await this.storage.runId; if (runId) { const attachLabelsPromise = this.zebrunnerClient.attachTestRunLabels(runId, labels); this.storage.addArtifactFinishPromise(attachLabelsPromise); } }; attachTestRunArtifactReferences = async (references) => { const runId = await this.storage.runId; if (runId) { const attachReferencesPromise = this.zebrunnerClient.attachTestRunArtifactReferences( runId, references, ); this.storage.addArtifactFinishPromise(attachReferencesPromise); } }; uploadTestRunArtifact = async (file) => { const runId = await this.storage.runId; if (runId) { const uploadArtifactPromise = this.zebrunnerClient.uploadTestRunArtifact(runId, file); this.storage.addArtifactFinishPromise(uploadArtifactPromise); } }; attachTestLabels = async (test, labels) => { const runId = await this.storage.runId; if (runId) { const testId = await this.storage.getTestId(test.uniqueId); const attachLabelsPromise = this.zebrunnerClient.attachTestLabels(runId, testId, labels); this.storage.addArtifactFinishPromise(attachLabelsPromise); } }; attachTestArtifactReferences = async (test, references) => { const runId = await this.storage.runId; if (runId) { const testId = await this.storage.getTestId(test.uniqueId); const attachReferencesPromise = this.zebrunnerClient.attachTestArtifactReferences( runId, testId, references, ); this.storage.addArtifactFinishPromise(attachReferencesPromise); } }; uploadTestArtifact = async (test, file) => { const runId = await this.storage.runId; if (runId) { const testId = await this.storage.getTestId(test.uniqueId); const uploadArtifactPromise = this.zebrunnerClient.uploadTestArtifact(runId, testId, file); this.storage.addArtifactFinishPromise(uploadArtifactPromise); } }; setTestMaintainer = async (test, maintainer) => { const runId = await this.storage.runId; if (runId) { const testId = await this.storage.getTestId(test.uniqueId); const testUpdatePromise = this.zebrunnerClient.setTestMaintainer(runId, testId, maintainer); this.storage.setTestUpdatePromise(test.uniqueId, testUpdatePromise); } }; revertTestRegistration = async (test) => { const runId = await this.storage.runId; if (runId) { const testId = await this.storage.getTestId(test.uniqueId); const testRevertPromise = this.zebrunnerClient.revertTestRegistration(runId, testId); this.storage.setTestRevertPromise(test.uniqueId, testRevertPromise); } }; addTestCase = async (test, testCase) => { const testId = await this.storage.getTestId(test.uniqueId); if (testId) { this.storage.addTestTestCase(test.uniqueId, testCase); } }; sendLogs = async (test, logs) => { const runId = await this.storage.runId; if (runId) { const testId = await this.storage.getTestId(test.uniqueId); const sendLogsPromise = this.zebrunnerClient.sendLogs(runId, testId, logs); this.storage.addArtifactFinishPromise(sendLogsPromise); } }; } module.exports = ZebrunnerReporter;