Skip to content

Commit 258c088

Browse files
authored
Fix cypress workflow (#5)
* fix cypress workflow * skip if no GA * fix
1 parent 8caa5d6 commit 258c088

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

.github/workflows/cypress.yml

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,6 @@ on:
55
branches: [develop, dependabot/**, "*test*", "*cypress*"]
66

77
jobs:
8-
# ── Wait for Vercel preview deployment before running tests ──────────────────
9-
# Vercel deploys on every push; we wait for it rather than building locally.
10-
wait-for-vercel:
11-
name: Wait for Vercel deployment
12-
runs-on: ubuntu-24.04
13-
permissions:
14-
deployments: read
15-
statuses: read
16-
outputs:
17-
preview_url: ${{ steps.wait.outputs.url }}
18-
steps:
19-
- name: Wait for Vercel preview deployment to be ready
20-
uses: patrickedqvist/wait-for-vercel-preview@v1.3.3
21-
id: wait
22-
with:
23-
token: ${{ secrets.GITHUB_TOKEN }}
24-
max_timeout: 1000
25-
check_interval: 5
26-
278
# ── Discover spec files dynamically ─────────────────────────────────────────
289
# Outputs a JSON array of spec paths so the test matrix is always in sync
2910
# with the filesystem — no manual updates needed when specs are added.
@@ -41,15 +22,14 @@ jobs:
4122
specs=$(find cypress/e2e -name "*.cy.ts" | sort | jq -R -s -c 'split("\n")[:-1]')
4223
echo "specs=$specs" >> $GITHUB_OUTPUT
4324
44-
# ── One container per spec, run against the Vercel preview URL ───────────────
25+
# ── One container per spec, built and served locally ─────────────────────────
4526
cypress:
4627
name: "Cypress (${{ matrix.spec }})"
47-
needs: [wait-for-vercel, discover]
28+
needs: [discover]
4829
runs-on: ubuntu-24.04
4930
permissions:
5031
contents: read
5132
checks: write
52-
deployments: read
5333
strategy:
5434
fail-fast: false
5535
matrix:
@@ -70,9 +50,12 @@ jobs:
7050
headed: false
7151
spec: ${{ matrix.spec }}
7252
config: video=false
53+
build: npm run build
54+
start: npm run start
55+
wait-on: http://localhost:3000
7356
env:
74-
CYPRESS_BASE_URL: ${{ needs.wait-for-vercel.outputs.preview_url }}
7557
NEXT_PUBLIC_GA_ID: ${{ secrets.NEXT_PUBLIC_GA_ID }}
58+
CYPRESS_NEXT_PUBLIC_GA_ID: ${{ secrets.NEXT_PUBLIC_GA_ID }}
7659

7760
- uses: actions/upload-artifact@v7
7861
if: failure()

cypress/e2e/cookie-consent.cy.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
const COOKIE = "chaynCookieConsent";
1313
const GA4_SCRIPT = 'script[src*="googletagmanager.com/gtag/js"]';
1414

15+
// Skip GA4-loading assertions when NEXT_PUBLIC_GA_ID is not configured (e.g. fresh forks).
16+
// Tests that assert GA4 does NOT load are unaffected and will still run.
17+
const skipIfNoGA = function (this: Mocha.Context) {
18+
if (!Cypress.env("NEXT_PUBLIC_GA_ID")) this.skip();
19+
};
20+
1521
describe("Cookie consent", () => {
1622
it("shows the banner on first visit", () => {
1723
cy.clearCookies();
@@ -26,13 +32,14 @@ describe("Cookie consent", () => {
2632
cy.get(GA4_SCRIPT).should("not.exist");
2733
});
2834

29-
it("accept — hides banner, sets accepted cookie with path=/, loads GA4", () => {
35+
it("accept — hides banner, sets accepted cookie with path=/, loads GA4", function () {
3036
cy.clearCookies();
3137
cy.visit("/");
3238
cy.get('button[aria-label="Accept cookies"]').click();
3339
cy.contains("We use analytics cookies").should("not.exist");
3440
cy.getCookie(COOKIE).should("have.property", "value", "accepted");
3541
cy.getCookie(COOKIE).should("have.property", "path", "/");
42+
skipIfNoGA.call(this);
3643
cy.get(GA4_SCRIPT, { timeout: 6000 }).should("exist");
3744
});
3845

@@ -47,7 +54,8 @@ describe("Cookie consent", () => {
4754
cy.get(GA4_SCRIPT).should("not.exist");
4855
});
4956

50-
it("returning accepted visitor — no banner, GA4 auto-loads", () => {
57+
it("returning accepted visitor — no banner, GA4 auto-loads", function () {
58+
skipIfNoGA.call(this);
5159
cy.clearCookies();
5260
cy.setCookie(COOKIE, "accepted", { path: "/" });
5361
cy.visit("/");
@@ -64,7 +72,8 @@ describe("Cookie consent", () => {
6472
cy.get(GA4_SCRIPT).should("not.exist");
6573
});
6674

67-
it("consent revoked — cookie is cleared and banner reappears on reload", () => {
75+
it("consent revoked — cookie is cleared and banner reappears on reload", function () {
76+
skipIfNoGA.call(this);
6877
cy.setCookie(COOKIE, "accepted", { path: "/" });
6978
cy.visit("/");
7079
cy.get(GA4_SCRIPT, { timeout: 6000 }).should("exist");
@@ -151,7 +160,8 @@ describe("Cookie settings button", () => {
151160
cy.get(SETTINGS_BTN).should("be.visible");
152161
});
153162

154-
it("GA4 script is neutered after revoking consent mid-session", () => {
163+
it("GA4 script is neutered after revoking consent mid-session", function () {
164+
skipIfNoGA.call(this);
155165
cy.setCookie(COOKIE, "accepted", { path: "/" });
156166
cy.visit("/");
157167
cy.get(GA4_SCRIPT, { timeout: 6000 }).should("exist");

cypress/support/e2e.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@
33

44
// Import default Cypress commands (visit, get, click, etc.)
55
import "./commands";
6+
7+
// When analytics providers are not configured (e.g. NEXT_PUBLIC_GA_ID unset on a
8+
// fresh fork), @vercel/analytics can throw an unhandled promise rejection in
9+
// non-Vercel environments. Suppress these so missing secrets don't fail CI, but
10+
// only when GA is not configured — preserving error detection on full deployments.
11+
if (!Cypress.env("NEXT_PUBLIC_GA_ID")) {
12+
Cypress.on("uncaught:exception", () => false);
13+
}

0 commit comments

Comments
 (0)