Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

root-scripts.spec.ts

61 lines2.0 KB

1import { expect } from '@playwright/test'
2import { test } from '@tanstack/router-e2e-utils'
3​
4// All user-rendered scripts added in __root.tsx (not via head() API)
5const ALL_USER_SCRIPTS = [
6 'USER_SCRIPT', // <script src="user-script.js" /> after <Scripts />
7 'ASYNC_USER_SCRIPT', // <script src="async-user-script.js" async /> after <Scripts />
8 'HEAD_SCRIPT', // <script src="head-script.js" /> in <head>
9 'HEAD_ASYNC_SCRIPT', // <script src="head-async-script.js" async /> in <head>
10 'BEFORE_SCRIPTS_SCRIPT', // <script src="before-scripts-script.js" /> before <Scripts />
11 'BEFORE_SCRIPTS_ASYNC_SCRIPT', // <script src="before-scripts-async-script.js" async /> before <Scripts />
12] as const
13​
14test.describe('User-rendered scripts in __root.tsx', () => {
15 test('should not cause hydration errors on SSR load', async ({ page }) => {
16 const consoleErrors: Array<string> = []
17 page.on('console', (m) => {
18 if (m.type() === 'error') {
19 consoleErrors.push(m.text())
20 }
21 })
22​
23 await page.goto('/')
24​
25 // All user-rendered scripts should have executed
26 for (const scriptVar of ALL_USER_SCRIPTS) {
27 await page.waitForFunction(
28 (v) => (window as any)[v] === true,
29 scriptVar,
30 { timeout: 5000 },
31 )
32 }
33​
34 // Assert no hydration errors occurred
35 const hydrationErrors = consoleErrors.filter(
36 (e) =>
37 e.includes('Hydration') ||
38 e.includes('hydration') ||
39 e.includes("didn't match"),
40 )
41 expect(hydrationErrors).toEqual([])
42 })
43​
44 test('should execute on client-side navigation to another route', async ({
45 page,
46 }) => {
47 await page.goto('/posts')
48​
49 await page.getByRole('link', { name: 'Home' }).click()
50 await expect(page.getByText('Welcome Home!!!')).toBeInViewport()
51​
52 // Root layout scripts should have executed during the initial page load
53 for (const scriptVar of ALL_USER_SCRIPTS) {
54 await page.waitForFunction(
55 (v) => (window as any)[v] === true,
56 scriptVar,
57 { timeout: 5000 },
58 )
59 }
60 })
61})