@argos-ci/cypress
Version:
Cypress SDK for visual testing with Argos.
86 lines (85 loc) • 3.25 kB
JavaScript
import { upload } from "@argos-ci/core";
import { dirname, extname, join } from "node:path";
import { copyFile } from "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 = join(argosScreenshotsDir, details.name.slice(6) + extname(details.path));
await createDirectory(dirname(newPath));
await 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 = join(argosScreenshotsDir, relativePath);
await createDirectory(dirname(newPath));
await copyFile(details.path, newPath);
return { 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 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
export { argosAfterRun, argosAfterScreenshot, registerArgosTask };