Skip to content

Commit

Permalink
fix(cypress): improve compatibility with bundlers
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Dec 15, 2023
1 parent ad3ddc6 commit 366b39c
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 49 deletions.
5 changes: 3 additions & 2 deletions build/rollup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { swc } from "rollup-plugin-swc3";
import ts from "rollup-plugin-ts";
import json from "@rollup/plugin-json";

export const swcPlugin = swc({
tsconfig: false,
Expand Down Expand Up @@ -31,7 +32,7 @@ export const buildEs = ({
file: output,
format: "es",
},
plugins: [swcPlugin],
plugins: [json(), swcPlugin],
});

export const buildTypes = ({
Expand All @@ -41,9 +42,9 @@ export const buildTypes = ({
} = {}) => ({
input,
external,
plugins: [tsPlugin],
output: {
file: output,
format: "es",
},
plugins: [json(), tsPlugin],
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"private": true,
"type": "module",
"devDependencies": {
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-swc": "^0.2.1",
"@rollup/plugin-typescript": "^11.1.5",
"@swc/core": "^1.3.93",
Expand Down
4 changes: 4 additions & 0 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"types": "./dist/global.d.ts",
"default": "./dist/global.js"
},
"./cypress.cjs": {
"types": "./dist/cypress.d.ts",
"default": "./dist/cypress.cjs"
},
"./package.json": "./package.json"
},
"types": "./dist/index.d.ts",
Expand Down
20 changes: 20 additions & 0 deletions packages/browser/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const bundleGlobal = (config) => ({
...config,
});

const bundleCypress = (config) => ({
input: "src/cypress.ts",
external: ignoreRelative,
...config,
});

export default [
buildEs(),
buildTypes(),
Expand All @@ -29,4 +35,18 @@ export default [
},
plugins: [tsPlugin],
}),
bundleCypress({
output: {
file: `dist/cypress.cjs`,
format: "cjs",
},
plugins: [swcPlugin],
}),
bundleCypress({
output: {
file: `dist/cypress.d.ts`,
format: "es",
},
plugins: [tsPlugin],
}),
];
3 changes: 3 additions & 0 deletions packages/browser/src/cypress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getGlobalFilePath() {
return require.resolve("@argos-ci/browser/global.js");
}
79 changes: 32 additions & 47 deletions packages/cypress/src/support.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import "cypress-wait-until";
import type { ArgosGlobal } from "@argos-ci/browser/global.js";
import { resolveViewport, ViewportOption } from "@argos-ci/browser";
import { resolveViewport, type ViewportOption } from "@argos-ci/browser";
import { getGlobalFilePath } from "@argos-ci/browser/cypress.cjs";
import {
getMetadataPath,
getScreenshotName,
ScreenshotMetadata,
} from "@argos-ci/util/browser";
// @ts-ignore
import { version } from "../package.json";

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
Expand Down Expand Up @@ -39,26 +42,13 @@ declare global {
function injectArgos() {
cy.window({ log: false }).then((window) => {
if (typeof (window as any).__ARGOS__ !== "undefined") return;
const fileName =
typeof require.resolve === "function"
? require.resolve("@argos-ci/browser/global.js")
: "node_modules/@argos-ci/browser/dist/global.js";
const fileName = getGlobalFilePath();
return cy.readFile<string>(fileName).then((source) => {
window.eval(source);
});
});
}

function readArgosCypressVersion() {
const fileName =
typeof require.resolve === "function"
? require.resolve("@argos-ci/cypress/package.json")
: "node_modules/@argos-ci/cypress/package.json";
return cy.readFile(fileName).then((source) => {
return source.version;
});
}

Cypress.Commands.add(
"argosScreenshot",
{ prevSubject: ["optional", "element", "window", "document"] },
Expand Down Expand Up @@ -110,38 +100,33 @@ Cypress.Commands.add(
(window as any).__ARGOS__ as ArgosGlobal
).getColorScheme();

readArgosCypressVersion().then((argosCypressVersion) => {
const metadata: ScreenshotMetadata = {
url: window.location.href,
viewport: {
width: window.innerWidth,
height: window.innerHeight,
},
colorScheme,
mediaType,
test: {
title: Cypress.currentTest.title,
titlePath: Cypress.currentTest.titlePath,
},
browser: {
name: Cypress.browser.name,
version: Cypress.browser.version,
},
automationLibrary: {
name: "cypress",
version: Cypress.version,
},
sdk: {
name: "@argos-ci/cypress",
version: argosCypressVersion,
},
};

cy.writeFile(
getMetadataPath(ref.props.path),
JSON.stringify(metadata),
);
});
const metadata: ScreenshotMetadata = {
url: window.location.href,
viewport: {
width: window.innerWidth,
height: window.innerHeight,
},
colorScheme,
mediaType,
test: {
title: Cypress.currentTest.title,
titlePath: Cypress.currentTest.titlePath,
},
browser: {
name: Cypress.browser.name,
version: Cypress.browser.version,
},
automationLibrary: {
name: "cypress",
version: Cypress.version,
},
sdk: {
name: "@argos-ci/cypress",
version,
},
};

cy.writeFile(getMetadataPath(ref.props.path), JSON.stringify(metadata));
});
}

Expand Down
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"paths": {
"@argos-ci/*": ["packages/*/src"],
"@argos-ci/browser/global.js": ["packages/browser/src/global.js"],
"@argos-ci/browser/cypress.cjs": ["packages/browser/src/cypress.js"],
"@argos-ci/util/browser": ["packages/util/src/browser.js"],
"@argos-ci/playwright/reporter": ["packages/playwright/src/reporter.js"]
},
Expand Down

0 comments on commit 366b39c

Please sign in to comment.