1- import { expect , request } from '@playwright/test'
1+ import { expect } from '@playwright/test'
22import { test } from '@tanstack/router-e2e-utils'
33
44// Whitelist errors that can occur in CI:
@@ -13,21 +13,24 @@ test.describe('CSS styles in SSR (dev mode)', () => {
1313 test . use ( { whitelistErrors } )
1414
1515 // Warmup: trigger Vite's dependency optimization before running tests
16- // This prevents "504 (Outdated Optimize Dep)" errors during actual tests
17- test . beforeAll ( async ( { baseURL } ) => {
18- const context = await request . newContext ( )
16+ // This prevents "optimized dependencies changed. reloading" during actual tests
17+ // We use a real browser context since dep optimization happens on JS load, not HTTP requests
18+ test . beforeAll ( async ( { browser, baseURL } ) => {
19+ const context = await browser . newContext ( )
20+ const page = await context . newPage ( )
1921 try {
20- // Hit both pages to trigger any dependency optimization
21- await context . get ( baseURL ! )
22- await context . get ( `${ baseURL } /modules` )
23- // Give Vite time to complete optimization
24- await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
25- // Hit again after optimization
26- await context . get ( baseURL ! )
22+ // Load both pages to trigger dependency optimization
23+ await page . goto ( baseURL ! )
24+ await page . waitForTimeout ( 2000 ) // Wait for deps to optimize
25+ await page . goto ( `${ baseURL } /modules` )
26+ await page . waitForTimeout ( 2000 )
27+ // Load again after optimization completes
28+ await page . goto ( baseURL ! )
29+ await page . waitForTimeout ( 1000 )
2730 } catch {
2831 // Ignore errors during warmup
2932 } finally {
30- await context . dispose ( )
33+ await context . close ( )
3134 }
3235 } )
3336
@@ -115,14 +118,17 @@ test.describe('CSS styles in SSR (dev mode)', () => {
115118 test ( 'styles persist after hydration' , async ( { page, baseURL } ) => {
116119 await page . goto ( buildUrl ( baseURL ! , '/' ) )
117120
118- // Wait for hydration
119- await page . waitForTimeout ( 1000 )
120-
121+ // Wait for hydration and styles to be applied
121122 const element = page . getByTestId ( 'global-styled' )
122- const backgroundColor = await element . evaluate (
123- ( el ) => getComputedStyle ( el ) . backgroundColor ,
124- )
125- expect ( backgroundColor ) . toBe ( 'rgb(59, 130, 246)' )
123+ await expect ( element ) . toBeVisible ( )
124+
125+ // Wait for CSS to be applied (background color should not be transparent)
126+ await expect ( async ( ) => {
127+ const backgroundColor = await element . evaluate (
128+ ( el ) => getComputedStyle ( el ) . backgroundColor ,
129+ )
130+ expect ( backgroundColor ) . toBe ( 'rgb(59, 130, 246)' )
131+ } ) . toPass ( { timeout : 5000 } )
126132 } )
127133
128134 test ( 'CSS modules styles persist after hydration' , async ( {
@@ -131,14 +137,17 @@ test.describe('CSS styles in SSR (dev mode)', () => {
131137 } ) => {
132138 await page . goto ( buildUrl ( baseURL ! , '/modules' ) )
133139
134- // Wait for hydration
135- await page . waitForTimeout ( 1000 )
136-
140+ // Wait for hydration and styles to be applied
137141 const card = page . getByTestId ( 'module-card' )
138- const backgroundColor = await card . evaluate (
139- ( el ) => getComputedStyle ( el ) . backgroundColor ,
140- )
141- expect ( backgroundColor ) . toBe ( 'rgb(240, 253, 244)' )
142+ await expect ( card ) . toBeVisible ( )
143+
144+ // Wait for CSS to be applied (background color should not be transparent)
145+ await expect ( async ( ) => {
146+ const backgroundColor = await card . evaluate (
147+ ( el ) => getComputedStyle ( el ) . backgroundColor ,
148+ )
149+ expect ( backgroundColor ) . toBe ( 'rgb(240, 253, 244)' )
150+ } ) . toPass ( { timeout : 5000 } )
142151 } )
143152
144153 test ( 'styles work correctly after client-side navigation' , async ( {
0 commit comments