UNPKG

@qualitywatcher/codeceptjs-reporter

Version:
172 lines (171 loc) 7.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const date_fns_1 = require("date-fns"); const codeceptjs_1 = require("codeceptjs"); const qualitywatcher_interface_1 = require("./qualitywatcher.interface"); const qualitywatcher_1 = require("./qualitywatcher"); const util_1 = require("./util"); const helpers = codeceptjs_1.container.helpers(); const supportedHelpers = [ 'WebDriver', 'Appium', 'Nightmare', 'Puppeteer', 'Playwright', 'TestCafe', 'REST' ]; const defaultReporterOptions = { projectId: null, description: '', testRunName: '', complete: false, includeAllCases: false, includeCaseWithoutId: false, }; let helper; for (const helperName of supportedHelpers) { if (Object.keys(helpers).indexOf(helperName) > -1) { helper = helpers[helperName]; } } module.exports = function (config) { const options = Object.assign(defaultReporterOptions, config.reporterOptions); (0, util_1.checkForEnvironmentalVariables)(); if (options.projectId === 0 || options.projectId === undefined) throw new Error('Please provide QualityWatcher projectId to the reporter options'); if (options.testRunName === '') options.testRunName = `New test run on${(0, date_fns_1.format)(new Date(), 'yyyy-MM-dd HH:mm:ss')};`; if (options.description === '') options.description = 'CodeceptJS Test Run'; let failedTests = []; const passedTests = []; const errors = {}; const prefixTag = "@S"; const prefixTagReg = /S\d+C\d+/; const defaultElapsedTime = '1000'; const ids = []; codeceptjs_1.event.dispatcher.on(codeceptjs_1.event.test.started, async (test) => { if (test.body) { if (test.body.includes('addExampleInTable')) { const qTag = /"qualitywatcherTag":"(@S\d+C\d+)"/.exec(test.title); if (qTag) { test.tags.push(qTag[1]); } } } test.startTime = Date.now(); }); const failedTestCaseIds = new Set(); codeceptjs_1.event.dispatcher.on(codeceptjs_1.event.test.failed, async (test, err) => { test.endTime = Date.now(); test.elapsed = Math.round(test.endTime - test.startTime); if (test.tags.length === 0 && options.includeCaseWithoutId) { // add testDetails const data = (0, util_1.createTestData)({ case: { suiteTitle: test.parent.title || 'CodeceptJS', testCaseTitle: test.title, steps: (0, util_1.humanizeStep)(test.step), }, status: qualitywatcher_interface_1.Status.Failed, duration: test.elapsed === 0 ? defaultElapsedTime : test.elapsed, error: err.message, }); failedTests.push(data); } else { test.tags.forEach((tag) => { const qIds = (0, util_1.getSuiteAndCaseIds)(tag); if (prefixTagReg.test(tag)) { if (!failedTestCaseIds.has(qIds.test_id)) { // else it also failed on retry so we shouldnt add in a duplicate failedTestCaseIds.add({ ...qIds, }); // add testDetails const data = (0, util_1.createTestData)({ testId: qIds.test_id, suiteId: qIds.suite_id, status: qualitywatcher_interface_1.Status.Failed, duration: test.elapsed === 0 ? defaultElapsedTime : test.elapsed, error: err.message, }); failedTests.push(data); } errors[tag.split(prefixTag)[1]] = err; } }); } }); codeceptjs_1.event.dispatcher.on(codeceptjs_1.event.test.passed, (test) => { test.endTime = Date.now(); test.elapsed = Math.round(test.endTime - test.startTime); if (test.tags.length === 0 && options.includeCaseWithoutId) { // add testDetails const data = (0, util_1.createTestData)({ case: { suiteTitle: test.parent.title || 'CodeceptJS', testCaseTitle: test.title, steps: (0, util_1.humanizeStep)(test.steps), }, status: qualitywatcher_interface_1.Status.Passed, duration: test.elapsed === 0 ? defaultElapsedTime : test.elapsed, }); passedTests.push(data); } else { test.tags.forEach(tag => { if (tag.includes(prefixTag)) { const qIds = (0, util_1.getSuiteAndCaseIds)(tag); // remove duplicates caused by retries if (failedTestCaseIds.has({ test_id: qIds.test_id, })) { failedTests = failedTests.filter(({ test_id }) => test_id !== qIds.test_id); } // add testDetails const data = (0, util_1.createTestData)({ testId: qIds.test_id, suiteId: qIds.suite_id, status: qualitywatcher_interface_1.Status.Passed, duration: test.elapsed === 0 ? defaultElapsedTime : test.elapsed, }); passedTests.push(data); } }); } }); codeceptjs_1.event.dispatcher.once(codeceptjs_1.event.all.after, async () => { const mergedTests = failedTests.concat(passedTests); if (mergedTests.length === 0) { codeceptjs_1.output.plugin('QualityWatcher', 'No tests found'); return; } const payload = getPayload(options, mergedTests); const qualityWatcherOptions = { password: process.env.QUALITYWATCHER_API_KEY, projectId: options === null || options === void 0 ? void 0 : options.projectId, }; try { const qualitywatcher = new qualitywatcher_1.QualityWatcher(qualityWatcherOptions); const response = await qualitywatcher.publishResults(payload); } catch (err) { codeceptjs_1.output.error(err); } }); }; function getPayload(reporterOptions, results) { const suites = [...new Set(results.map((result) => result === null || result === void 0 ? void 0 : result.suite_id))].filter(Boolean); const body = { projectId: Number(reporterOptions === null || reporterOptions === void 0 ? void 0 : reporterOptions.projectId), include_all_cases: reporterOptions === null || reporterOptions === void 0 ? void 0 : reporterOptions.includeAllCases, testRunName: `${reporterOptions === null || reporterOptions === void 0 ? void 0 : reporterOptions.testRunName} automated test run - ${new Date()}`, description: reporterOptions === null || reporterOptions === void 0 ? void 0 : reporterOptions.description, suites, results: results, complete: reporterOptions === null || reporterOptions === void 0 ? void 0 : reporterOptions.complete, }; return body; }