1212const COOKIE = "chaynCookieConsent" ;
1313const 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+
1521describe ( "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" ) ;
0 commit comments