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

app.spec.ts

90 lines3.3 KB

1import { existsSync, readdirSync } from 'node:fs'
2import { join } from 'node:path'
3import { expect } from '@playwright/test'
4import { test } from '@tanstack/router-e2e-utils'
5​
6test.describe('Static Server Functions with Nitro', () => {
7 test.describe('Build Output Verification', () => {
8 test('should write staticServerFnCache files to the public output directory', () => {
9 const publicDir = join(process.cwd(), '.output', 'public')
10​
11 // The static server function cache should be in the public directory
12 // (the directory served by Nitro), NOT in .output/dist/client/
13 const cacheDir = join(publicDir, '__tsr', 'staticServerFnCache')
14 expect(existsSync(cacheDir)).toBe(true)
15​
16 // There should be JSON cache files
17 const files = readdirSync(cacheDir)
18 const jsonFiles = files.filter((f) => f.endsWith('.json'))
19 expect(jsonFiles.length).toBeGreaterThan(0)
20 })
21​
22 test('should NOT have staticServerFnCache in dist/client', () => {
23 // The bug: cache files end up in .output/dist/client/ instead of .output/public/
24 const wrongDir = join(
25 process.cwd(),
26 '.output',
27 'dist',
28 'client',
29 '__tsr',
30 'staticServerFnCache',
31 )
32 expect(existsSync(wrongDir)).toBe(false)
33 })
34​
35 test('should have prerendered HTML pages in public directory', () => {
36 const publicDir = join(process.cwd(), '.output', 'public')
37 expect(existsSync(join(publicDir, 'index.html'))).toBe(true)
38 expect(existsSync(join(publicDir, 'posts', 'index.html'))).toBe(true)
39 })
40 })
41​
42 test.describe('Runtime Verification', () => {
43 test('should render the home page with data from static server function', async ({
44 page,
45 }) => {
46 await page.goto('/')
47 await expect(page.getByTestId('index-heading')).toHaveText('Home')
48 await expect(page.getByTestId('index-message')).toHaveText(
49 'Hello from static server function!',
50 )
51 })
52​
53 test('should render the posts page with data from static server function', async ({
54 page,
55 }) => {
56 await page.goto('/posts')
57 await expect(page.getByTestId('posts-heading')).toHaveText('Posts')
58 await expect(page.getByTestId('post-1')).toHaveText('First Post')
59 await expect(page.getByTestId('post-2')).toHaveText('Second Post')
60 await expect(page.getByTestId('post-3')).toHaveText('Third Post')
61 })
62​
63 test('should decode a cached RawStream during client navigation', async ({
64 page,
65 }) => {
66 await page.goto('/')
67 // Nitro snapshots public assets before Start prerenders (see #6787).
68 // Serve the generated file as a static host would to isolate decoding.
69 await page.route('**/__tsr/staticServerFnCache/*.json', (route) =>
70 route.fulfill({
71 path: join(
72 process.cwd(),
73 '.output',
74 'public',
75 new URL(route.request().url()).pathname,
76 ),
77 }),
78 )
79 const cacheResponse = page.waitForResponse((response) =>
80 response.url().includes('/__tsr/staticServerFnCache/'),
81 )
82 await page.getByTestId('link-raw-stream').click()
83 const response = await cacheResponse
84 expect(response.status(), response.url()).toBe(200)
85 await expect(page.getByTestId('raw-stream')).toHaveText(
86 'Static cache stream',
87 )
88 })
89 })
90})