-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
index.test.js
219 lines (184 loc) · 6.04 KB
/
index.test.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { join } from 'path'
import getPort from 'get-port'
import cheerio from 'cheerio'
import {
initNextServerScript,
killApp,
renderViaHTTP,
fetchViaHTTP,
check,
File,
nextBuild,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const indexPg = new File(join(appDir, 'pages/index.js'))
let appPort
let server
const context = {}
const startServer = async (optEnv = {}, opts) => {
const scriptPath = join(appDir, 'server.js')
context.appPort = appPort = await getPort()
const env = Object.assign(
{ ...process.env },
{ PORT: `${appPort}`, __NEXT_TEST_MODE: 'true' },
optEnv
)
server = await initNextServerScript(
scriptPath,
/ready on/i,
env,
/ReferenceError: options is not defined/,
opts
)
}
describe('Custom Server', () => {
describe('with dynamic assetPrefix', () => {
beforeAll(() => startServer())
afterAll(() => killApp(server))
it('should serve internal file from render', async () => {
const data = await renderViaHTTP(appPort, '/static/hello.txt')
expect(data).toMatch(/hello world/)
})
it('should handle render with undefined query', async () => {
expect(await renderViaHTTP(appPort, '/no-query')).toMatch(/"query":/)
})
it('should set the assetPrefix dynamically', async () => {
const normalUsage = await renderViaHTTP(appPort, '/asset')
expect(normalUsage).not.toMatch(/127\.0\.0\.1/)
const dynamicUsage = await renderViaHTTP(
appPort,
'/asset?setAssetPrefix=1'
)
expect(dynamicUsage).toMatch(/127\.0\.0\.1/)
})
it('should handle null assetPrefix accordingly', async () => {
const normalUsage = await renderViaHTTP(
appPort,
'/asset?setEmptyAssetPrefix=1'
)
expect(normalUsage).toMatch(/"\/_next/)
})
it('should set the assetPrefix to a given request', async () => {
for (let lc = 0; lc < 1000; lc++) {
const [normalUsage, dynamicUsage] = await Promise.all([
await renderViaHTTP(appPort, '/asset'),
await renderViaHTTP(appPort, '/asset?setAssetPrefix=1'),
])
expect(normalUsage).not.toMatch(/127\.0\.0\.1/)
expect(dynamicUsage).toMatch(/127\.0\.0\.1/)
}
})
it('should render nested index', async () => {
const html = await renderViaHTTP(appPort, '/dashboard')
expect(html).toMatch(/made it to dashboard/)
})
it('should contain customServer in NEXT_DATA', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').text()).customServer).toBe(true)
})
})
describe('with generateEtags enabled', () => {
beforeAll(async () => {
await nextBuild(appDir)
await startServer({ GENERATE_ETAGS: 'true', NODE_ENV: 'production' })
})
afterAll(() => killApp(server))
it('response includes etag header', async () => {
const response = await fetchViaHTTP(appPort, '/')
expect(response.headers.get('etag')).toBeTruthy()
})
})
describe('with generateEtags disabled', () => {
beforeAll(() => startServer({ GENERATE_ETAGS: 'false' }))
afterAll(() => killApp(server))
it('response does not include etag header', async () => {
const response = await fetchViaHTTP(appPort, '/')
expect(response.headers.get('etag')).toBeNull()
})
})
describe('HMR with custom server', () => {
beforeAll(() => startServer())
afterAll(() => {
killApp(server)
indexPg.restore()
})
it('Should support HMR when rendering with /index pathname', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/test-index-hmr')
const text = await browser.elementByCss('#go-asset').text()
const logs = await browser.log()
expect(text).toBe('Asset')
// Hydrates with react 18 is correct as expected
expect(
logs.some((log) =>
log.message.includes(
'ReactDOM.hydrate is no longer supported in React 18'
)
)
).toBe(false)
indexPg.replace('Asset', 'Asset!!')
await check(() => browser.elementByCss('#go-asset').text(), /Asset!!/)
} finally {
if (browser) {
await browser.close()
}
}
})
})
describe('Error when rendering without starting slash', () => {
afterEach(() => killApp(server))
it('should warn in dev mode', async () => {
let stderr = ''
await startServer(
{},
{
onStderr(msg) {
stderr += msg || ''
},
}
)
const html = await renderViaHTTP(appPort, '/no-slash')
expect(html).toContain('made it to dashboard')
expect(stderr).toContain('Cannot render page with path "dashboard"')
})
it('should warn in production mode', async () => {
const { code } = await nextBuild(appDir)
expect(code).toBe(0)
let stderr = ''
await startServer(
{ NODE_ENV: 'production' },
{
onStderr(msg) {
stderr += msg || ''
},
}
)
const html = await renderViaHTTP(appPort, '/no-slash')
expect(html).toContain('made it to dashboard')
expect(stderr).toContain('Cannot render page with path "dashboard"')
})
})
describe('compression handling', function () {
beforeAll(() => startServer())
afterAll(() => killApp(server))
it.each(['/', '/no-query'])(
'should handle compression for route %s',
async (route) => {
const response = await fetchViaHTTP(appPort, route)
expect(response.headers.get('Content-Encoding')).toBe('gzip')
}
)
})
describe('with a custom fetch polyfill', () => {
beforeAll(() => startServer({ POLYFILL_FETCH: 'true' }))
afterAll(() => killApp(server))
it('should serve internal file from render', async () => {
const data = await renderViaHTTP(appPort, '/static/hello.txt')
expect(data).toMatch(/hello world/)
})
})
})