UNPKG

@argos-ci/cypress

Version:

Cypress SDK for visual testing with Argos.

86 lines (83 loc) 2.87 kB
// src/task.ts import { upload } from "@argos-ci/core"; import { extname, join, dirname } from "path"; // src/shared.ts var NAME_PREFIX = "argos/"; // src/task.ts import { copyFile } from "fs/promises"; function checkIsCypressFailedResult(results) { return "status" in results && results.status === "failed"; } var screenshotsDirectoryPromise = void 0; async function getScreenshotsDirectory() { const { createTemporaryDirectory } = await import("@argos-ci/util"); if (!screenshotsDirectoryPromise) { screenshotsDirectoryPromise = createTemporaryDirectory(); } return screenshotsDirectoryPromise; } async function createDirectory(directory) { const { createDirectory: createDirectory2 } = await import("@argos-ci/util"); await createDirectory2(directory); } function registerArgosTask(on, config, options) { on("after:screenshot", async (details) => { const { uploadToArgos = true } = options || {}; if (!uploadToArgos) { return { path: details.path }; } const argosScreenshotsDir = await getScreenshotsDirectory(); if (details.name?.startsWith(NAME_PREFIX)) { const newPath2 = join( argosScreenshotsDir, details.name.slice(NAME_PREFIX.length) + extname(details.path) ); await createDirectory(dirname(newPath2)); await copyFile(details.path, newPath2); return { path: newPath2 }; } 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 = join(argosScreenshotsDir, relativePath); await createDirectory(dirname(newPath)); await copyFile(details.path, newPath); return { path: join(argosScreenshotsDir, relativePath) }; }); on("after:run", async (results) => { const { uploadToArgos = true } = options || {}; if (!uploadToArgos) { return; } const argosScreenshotsDir = await getScreenshotsDirectory(); const res = await upload({ ...options, files: ["**/*.png"], root: argosScreenshotsDir, metadata: { testReport: checkIsCypressFailedResult(results) ? { status: "failed" } : { status: "passed", stats: { startTime: results.startedTestsAt, duration: results.totalDuration } } } }); console.log(`\u2705 Argos build created: ${res.build.url}`); }); } export { registerArgosTask };