-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathglobal.setup.ts
More file actions
91 lines (77 loc) 路 3.11 KB
/
Copy pathglobal.setup.ts
File metadata and controls
91 lines (77 loc) 路 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { chromium } from '@playwright/test'
import {
e2eStartDummyServer,
getTestServerPort,
} from '@tanstack/router-e2e-utils'
import packageJson from '../../package.json' with { type: 'json' }
async function waitForServer(url: string) {
const start = Date.now()
while (Date.now() - start < 30_000) {
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), 5_000)
try {
const res = await fetch(url, {
redirect: 'manual',
signal: controller.signal,
})
if (res.ok) return
} catch {
// ignore aborted/network errors
} finally {
clearTimeout(timer)
}
await new Promise((r) => setTimeout(r, 250))
}
throw new Error(`Timed out waiting for dev server at ${url}`)
}
async function preOptimizeDevServer(baseURL: string) {
const browser = await chromium.launch()
const context = await browser.newContext()
const page = await context.newPage()
try {
await page.goto(`${baseURL}/`, { waitUntil: 'domcontentloaded' })
await page.getByTestId('global-styled').waitFor({ state: 'visible' })
await page.waitForLoadState('networkidle')
await page.goto(`${baseURL}/modules`, { waitUntil: 'domcontentloaded' })
await page.getByTestId('module-card').waitFor({ state: 'visible' })
await page.waitForLoadState('networkidle')
await page.goto(`${baseURL}/sass-mixin`, { waitUntil: 'domcontentloaded' })
await page.getByTestId('mixin-styled').waitFor({ state: 'visible' })
await page.waitForLoadState('networkidle')
// Exercise client-side navigation so Vite discovers any remaining deps
// that only load via the client router (not full-page navigations).
await page.goto(`${baseURL}/`, { waitUntil: 'domcontentloaded' })
await page.getByTestId('global-styled').waitFor({ state: 'visible' })
await page.waitForLoadState('networkidle')
await page.getByTestId('nav-modules').click()
await page.waitForURL('**/modules')
await page.getByTestId('module-card').waitFor({ state: 'visible' })
await page.waitForLoadState('networkidle')
await page.getByTestId('nav-home').click()
await page.waitForURL(/\/([^/]*)(\/)?($|\?)/)
await page.getByTestId('global-styled').waitFor({ state: 'visible' })
await page.waitForLoadState('networkidle')
// Ensure we end in a stable state. Vite's optimize step triggers a reload;
// this waits until no further navigations happen for a short window.
for (let i = 0; i < 40; i++) {
const currentUrl = page.url()
await page.waitForTimeout(250)
if (page.url() === currentUrl) {
await page.waitForTimeout(250)
if (page.url() === currentUrl) return
}
}
throw new Error('Dev server did not reach a stable URL after warmup')
} finally {
await context.close()
await browser.close()
}
}
export default async function setup() {
await e2eStartDummyServer(packageJson.name)
if (process.env.MODE !== 'dev') return
const port = await getTestServerPort(packageJson.name)
const baseURL = `http://localhost:${port}`
await waitForServer(baseURL)
await preOptimizeDevServer(baseURL)
}