forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.ts
164 lines (144 loc) · 4.43 KB
/
index.test.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { join } from 'path'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import {
fetchViaHTTP,
findPort,
initNextServerScript,
killApp,
renderViaHTTP,
} from 'next-test-utils'
const react18Deps = {
react: '^18.0.0',
'react-dom': '^18.0.0',
}
describe('react 18 streaming SSR in minimal mode', () => {
let next: NextInstance
beforeAll(async () => {
process.env.NEXT_PRIVATE_MINIMAL_MODE = '1'
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>streaming</p>
}
export async function getServerSideProps() {
return { props: {} }
}
`,
},
nextConfig: {
experimental: {
runtime: 'nodejs',
},
webpack(config, { nextRuntime }) {
const path = require('path')
const fs = require('fs')
const runtimeFilePath = path.join(__dirname, 'runtimes.txt')
let runtimeContent = ''
try {
runtimeContent = fs.readFileSync(runtimeFilePath, 'utf8')
runtimeContent += '\n'
} catch (_) {}
runtimeContent += nextRuntime || 'client'
fs.writeFileSync(runtimeFilePath, runtimeContent)
return config
},
},
dependencies: react18Deps,
})
})
afterAll(() => {
delete process.env.NEXT_PRIVATE_MINIMAL_MODE
next.destroy()
})
it('should pass correct nextRuntime values', async () => {
const content = await next.readFile('runtimes.txt')
expect(content.split('\n').sort()).toEqual(['client', 'edge', 'nodejs'])
})
it('should generate html response by streaming correctly', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('streaming')
})
it('should have generated a static 404 page', async () => {
expect(await next.readFile('.next/server/pages/404.html')).toBeTruthy()
const res = await fetchViaHTTP(next.url, '/non-existent')
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
})
})
describe('react 18 streaming SSR with custom next configs', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'streaming-ssr/pages')),
},
nextConfig: require(join(__dirname, 'streaming-ssr/next.config.js')),
dependencies: react18Deps,
installCommand: 'npm install',
})
})
afterAll(() => next.destroy())
it('should render styled-jsx styles in streaming', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('color:blue')
})
it('should redirect paths without trailing-slash and render when slash is appended', async () => {
const page = '/hello'
const redirectRes = await fetchViaHTTP(
next.url,
page,
{},
{ redirect: 'manual' }
)
const res = await fetchViaHTTP(next.url, page + '/')
const html = await res.text()
expect(redirectRes.status).toBe(308)
expect(res.status).toBe(200)
expect(html).toContain('hello nextjs')
expect(html).toContain('home')
})
it('should render multi-byte characters correctly in streaming', async () => {
const html = await renderViaHTTP(next.url, '/multi-byte')
expect(html).toContain('マルチバイト'.repeat(28))
})
})
describe('react 18 streaming SSR with custom server', () => {
let next
let server
let appPort
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'custom-server/pages')),
'server.js': new FileRef(join(__dirname, 'custom-server/server.js')),
},
nextConfig: require(join(__dirname, 'custom-server/next.config.js')),
dependencies: react18Deps,
})
await next.stop()
const testServer = join(next.testDir, 'server.js')
appPort = await findPort()
server = await initNextServerScript(
testServer,
/Listening/,
{
...process.env,
PORT: appPort,
},
undefined,
{
cwd: next.testDir,
}
)
})
afterAll(async () => {
await next.destroy()
if (server) await killApp(server)
})
it('should render page correctly under custom server', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toContain('streaming')
})
})