UNPKG

@argos-ci/cypress

Version:

Cypress SDK for visual testing with Argos.

89 lines (88 loc) 3.55 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); let _argos_ci_core = require("@argos-ci/core"); let node_path = require("node:path"); let node_fs_promises = require("node:fs/promises"); //#endregion //#region src/task.ts function checkIsCypressFailedResult(results) { return "status" in results && results.status === "failed"; } let screenshotsDirectoryPromise = void 0; /** * Get the path to the directory where screenshots will be stored. */ async function getScreenshotsDirectory() { const { createTemporaryDirectory } = await import("@argos-ci/util"); if (!screenshotsDirectoryPromise) screenshotsDirectoryPromise = createTemporaryDirectory(); return screenshotsDirectoryPromise; } /** * Create a directory if it does not exist. */ async function createDirectory(directory) { const { createDirectory } = await import("@argos-ci/util"); await createDirectory(directory); } function getArgosConfigFromOptions(options) { return { uploadToArgos: options?.uploadToArgos ?? true }; } /** * Cypress "after:screenshot" event handler. * - Move screenshots to Argos directory */ async function argosAfterScreenshot(config, details, options) { const { uploadToArgos } = getArgosConfigFromOptions(options); if (!uploadToArgos) return { path: details.path }; const argosScreenshotsDir = await getScreenshotsDirectory(); if (details.name?.startsWith("argos/")) { const newPath = (0, node_path.join)(argosScreenshotsDir, details.name.slice(6) + (0, node_path.extname)(details.path)); await createDirectory((0, node_path.dirname)(newPath)); await (0, node_fs_promises.copyFile)(details.path, newPath); return { path: newPath }; } const { screenshotsFolder } = config; if (!screenshotsFolder) throw new Error("Cypress screenshotsFolder is not defined. Please set it in your cypress.config.js"); if (!details.path.startsWith(screenshotsFolder)) throw new Error(`Cypress screenshot path ${details.path} does not start with the configured screenshotsFolder ${screenshotsFolder}. Please check your cypress.config.js.`); const relativePath = details.path.slice(screenshotsFolder.length + (screenshotsFolder.endsWith("/") ? 0 : 1)); const newPath = (0, node_path.join)(argosScreenshotsDir, relativePath); await createDirectory((0, node_path.dirname)(newPath)); await (0, node_fs_promises.copyFile)(details.path, newPath); return { path: (0, node_path.join)(argosScreenshotsDir, relativePath) }; } /** * Cypress "after:run" event handler. * - Upload screenshots to Argos */ async function argosAfterRun(_config, results, options) { const { uploadToArgos } = getArgosConfigFromOptions(options); if (!uploadToArgos) return; const argosScreenshotsDir = await getScreenshotsDirectory(); const res = await (0, _argos_ci_core.upload)({ ...options, files: ["**/*.png"], root: argosScreenshotsDir, metadata: { testReport: checkIsCypressFailedResult(results) ? { status: "failed" } : { status: "passed", stats: { startTime: results.startedTestsAt, duration: results.totalDuration } } } }); console.log(`✅ Argos build created: ${res.build.url}`); } /** * Register the Argos tasks for Cypress. */ function registerArgosTask(on, config, options) { on("after:screenshot", (details) => { return argosAfterScreenshot(config, details, options); }); on("after:run", async (results) => { return argosAfterRun(config, results, options); }); } //#endregion exports.argosAfterRun = argosAfterRun; exports.argosAfterScreenshot = argosAfterScreenshot; exports.registerArgosTask = registerArgosTask;