UNPKG

@uuv/cypress

Version:

A solution to facilitate the writing and execution of E2E tests understandable by any human being using cucumber(BDD) and cypress

119 lines (118 loc) 4.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupNodeEvents = setupNodeEvents; const cypress_cucumber_preprocessor_1 = require("@badeball/cypress-cucumber-preprocessor"); const esbuild_1 = require("@badeball/cypress-cucumber-preprocessor/esbuild"); const cypress_esbuild_preprocessor_1 = __importDefault(require("@bahmutov/cypress-esbuild-preprocessor")); const fs_1 = __importDefault(require("fs")); const event_1 = require("@uuv/runner-commons/runner/event"); const path_1 = __importDefault(require("path")); async function setupNodeEvents(cypressOn, config) { const startedFile = []; // https://github.com/bahmutov/cypress-on-fix const on = require("cypress-on-fix")(cypressOn); await (0, cypress_cucumber_preprocessor_1.addCucumberPreprocessorPlugin)(on, config); await event_1.UUVListenerHelper.build(); on("file:preprocessor", (0, cypress_esbuild_preprocessor_1.default)({ plugins: [ (0, esbuild_1.createEsbuildPlugin)(config), { name: "ignore-fs", setup(build) { build.onResolve({ filter: /^(fs|child_process)$/ }, () => ({ path: "fs", namespace: "ignore", })); build.onLoad({ filter: /.*/, namespace: "ignore" }, () => ({ contents: "module.exports = {}", loader: "js", })); } } ], alias: { path: "path-browserify" } })); on("before:run", async () => { // eslint-disable-next-line dot-notation const a11yReportFilePath = config.env["uuvOptions"].report.a11y.outputFile; // eslint-disable-next-line dot-notation const generateA11yReport = config.env["uuvOptions"].report.a11y.enabled; clearA11yReport(a11yReportFilePath); if (generateA11yReport === true) { await initA11yReport(a11yReportFilePath); } event_1.UUVEventEmitter.getInstance().emitProgressStart(); }); on("after:run", async (results) => { event_1.UUVEventEmitter.getInstance().emitProgressFinish(); }); on("before:spec", async (spec) => { if (!startedFile.includes(spec.absolute)) { event_1.UUVEventEmitter.getInstance().emitTestSuiteStarted({ testSuiteName: spec.name, testSuitelocation: spec.absolute }); startedFile.push(spec.absolute); } }); on("after:spec", async (spec, results) => { results?.tests?.forEach(test => { event_1.UUVEventEmitter.getInstance().emitTestStarted({ testName: test.title[1], testSuiteName: spec.name, testSuitelocation: spec.absolute }); if (test.state === "passed") { event_1.UUVEventEmitter.getInstance().emitTestFinished({ testName: test.title[1], testSuiteName: spec.name, duration: test.duration }); } else if (test.state === "failed") { event_1.UUVEventEmitter.getInstance().emitTestFailed({ testName: test.title[1], testSuiteName: spec.name, duration: test.duration }); } else { event_1.UUVEventEmitter.getInstance().emitTestIgnored({ testName: test.title[1], testSuiteName: spec.name }); } }); event_1.UUVEventEmitter.getInstance().emitTestSuiteFinished({ testSuiteName: spec.name }); }); function clearA11yReport(reportFilePath) { if (fs_1.default.existsSync(reportFilePath)) { fs_1.default.rmSync(reportFilePath); } } async function initA11yReport(reportFilePath) { // eslint-disable-next-line dot-notation const packageJson = await import(path_1.default.join(config.env["uuvOptions"].projectDir, "package.json")); const emptyReport = { app: { name: packageJson.name, description: packageJson.description, usecases: [] } }; fs_1.default.writeFileSync(reportFilePath, JSON.stringify(emptyReport, null, 4), { flag: "w" }); } // eslint-disable-next-line dot-notation config.env["a11yBundlePath"] = typeof require?.resolve === "function" ? require.resolve("@uuv/a11y/bundle") : "node_modules/@uuv/a11y/bundle/uuv-a11y.bundle.js"; // Make sure to return the config object as it might have been modified by the plugin. return config; }