forked from vercel/next.js
/
index.test.js
307 lines (260 loc) · 9.89 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* eslint-env jest */
/* global jasmine */
import webdriver from 'next-webdriver'
import { join } from 'path'
import cheerio from 'cheerio'
import { existsSync, readdirSync, readFileSync } from 'fs'
import {
killApp,
waitFor,
findPort,
nextBuild,
nextStart,
fetchViaHTTP,
renderViaHTTP,
} from 'next-test-utils'
import qs from 'querystring'
import path from 'path'
import fetch from 'node-fetch'
const appDir = join(__dirname, '../')
const serverlessDir = join(appDir, '.next/serverless/pages')
const chunksDir = join(appDir, '.next/static/chunks')
const buildIdFile = join(appDir, '.next/BUILD_ID')
let stderr = ''
let appPort
let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Serverless', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort, {
onStderr: msg => {
stderr += msg || ''
},
})
})
afterAll(() => killApp(app))
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})
it('should add autoExport for auto pre-rendered pages', async () => {
for (const page of ['/', '/abc']) {
const html = await renderViaHTTP(appPort, page)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.autoExport).toBe(true)
}
})
it('should not add autoExport for non pre-rendered pages', async () => {
for (const page of ['/fetch']) {
const html = await renderViaHTTP(appPort, page)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(!!data.autoExport).toBe(false)
}
})
it('should serve file from public folder', async () => {
const content = await renderViaHTTP(appPort, '/hello.txt')
expect(content.trim()).toBe('hello world')
const legacy = await renderViaHTTP(appPort, '/static/legacy.txt')
expect(legacy).toMatch(`new static folder`)
})
it('should not infinity loop on a 404 static file', async () => {
expect.assertions(2)
// ensure top-level static does not exist (important for test)
// we expect /public/static, though.
expect(existsSync(path.join(appDir, 'static'))).toBe(false)
const res = await fetchViaHTTP(appPort, '/static/404')
expect(res.status).toBe(404)
})
it('should render the page with dynamic import', async () => {
const html = await renderViaHTTP(appPort, '/dynamic')
expect(html).toMatch(/Hello!/)
})
it('should render the page with same dynamic import', async () => {
const html = await renderViaHTTP(appPort, '/dynamic-two')
expect(html).toMatch(/Hello!/)
})
it('should render 404', async () => {
const html = await renderViaHTTP(appPort, '/404')
expect(html).toMatch(/This page could not be found/)
})
it('should render 404 for /_next/static', async () => {
const html = await renderViaHTTP(appPort, '/_next/static')
expect(html).toMatch(/This page could not be found/)
})
it('should render an AMP page', async () => {
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/ampproject\.org/)
})
it('should have correct amphtml rel link', async () => {
const html = await renderViaHTTP(appPort, '/some-amp')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/rel="amphtml" href="\/some-amp\.amp"/)
})
it('should have correct canonical link', async () => {
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/rel="canonical" href="\/some-amp"/)
})
it('should have correct canonical link (auto-export link)', async () => {
const html = await renderViaHTTP(appPort, '/some-amp.amp')
expect(html).toMatch(/rel="canonical" href="\/some-amp"/)
})
it('should render correctly when importing isomorphic-unfetch', async () => {
const url = `http://localhost:${appPort}/fetch`
const res = await fetch(url)
expect(res.status).toBe(200)
const text = await res.text()
expect(text.includes('failed')).toBe(false)
})
it('should render correctly when importing isomorphic-unfetch on the client side', async () => {
const browser = await webdriver(appPort, '/')
try {
const text = await browser
.elementByCss('#fetchlink')
.click()
.waitForElementByCss('.fetch-page')
.elementByCss('#text')
.text()
expect(text).toMatch(/fetch page/)
} finally {
await browser.close()
}
})
it('should render correctly when importing isomorphic-unfetch CJS', async () => {
const url = `http://localhost:${appPort}/fetch-cjs`
const res = await fetch(url)
expect(res.status).toBe(200)
const text = await res.text()
expect(text.includes('failed')).toBe(false)
})
it('should render correctly when importing isomorphic-unfetch CJS on the client side', async () => {
const browser = await webdriver(appPort, '/')
try {
const text = await browser
.elementByCss('#fetchcjslink')
.click()
.waitForElementByCss('.fetch-cjs-page')
.elementByCss('#text')
.text()
expect(text).toMatch(/fetch page/)
} finally {
await browser.close()
}
})
it('should not have combined client-side chunks', () => {
expect(readdirSync(chunksDir).length).toBeGreaterThanOrEqual(2)
const buildId = readFileSync(buildIdFile, 'utf8').trim()
const pageContent = join(
appDir,
'.next/static',
buildId,
'pages/dynamic.js'
)
expect(readFileSync(pageContent, 'utf8')).not.toContain('Hello!')
})
it('should not output _app.js and _document.js to serverless build', () => {
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()
})
it('should replace static pages with HTML files', async () => {
const staticFiles = ['abc', 'dynamic', 'dynamic-two', 'some-amp']
for (const file of staticFiles) {
expect(existsSync(join(serverlessDir, file + '.html'))).toBe(true)
expect(existsSync(join(serverlessDir, file + '.js'))).toBe(false)
}
})
it('should not replace non-static pages with HTML files', async () => {
const nonStaticFiles = ['fetch', '_error']
for (const file of nonStaticFiles) {
expect(existsSync(join(serverlessDir, file + '.js'))).toBe(true)
expect(existsSync(join(serverlessDir, file + '.html'))).toBe(false)
}
})
it('should reply on API request successfully', async () => {
const content = await renderViaHTTP(appPort, '/api/hello')
expect(content).toMatch(/hello world/)
})
it('should reply on dynamic API request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1')
const { id } = JSON.parse(result)
expect(id).toBe('post-1')
})
it('should reply on dynamic API request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1?param=val')
const { id, param } = JSON.parse(result)
expect(id).toBe('post-1')
expect(param).toBe('val')
})
it('should reply on dynamic API index request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1')
const { path } = JSON.parse(result)
expect(path).toBe('post-1')
})
it('should reply on dynamic API index request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1?param=val')
const { path, param } = JSON.parse(result)
expect(path).toBe('post-1')
expect(param).toBe('val')
})
it('should 404 on API request with trailing slash', async () => {
const res = await fetchViaHTTP(appPort, '/api/hello/')
expect(res.status).toBe(404)
})
it('should have the correct query string for a dynamic route', async () => {
const paramRaw = 'test % 123'
const param = encodeURIComponent(paramRaw)
const html = await renderViaHTTP(appPort, `/dr/${param}`)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.query).toEqual({ slug: paramRaw })
})
it('should have the correct query string for a now route', async () => {
const paramRaw = 'test % 123'
const html = await fetchViaHTTP(appPort, `/dr/[slug]`, '', {
headers: {
'x-now-route-matches': qs.stringify({
1: encodeURIComponent(paramRaw),
}),
},
}).then(res => res.text())
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.query).toEqual({ slug: paramRaw })
})
it('should have the correct query string for a catch all now route', async () => {
const paramRaw = ['nested % 1', 'nested/2']
const html = await fetchViaHTTP(appPort, `/catchall/[...slug]`, '', {
headers: {
'x-now-route-matches': qs.stringify({
1: paramRaw.map(e => encodeURIComponent(e)).join('/'),
}),
},
}).then(res => res.text())
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.query).toEqual({ slug: paramRaw })
})
it('should log error in API route correctly', async () => {
await renderViaHTTP(appPort, '/api/top-level-error')
await waitFor(1000)
expect(stderr).toContain('top-level-oops')
})
describe('With basic usage', () => {
it('should allow etag header support', async () => {
const url = `http://localhost:${appPort}/`
const etag = (await fetch(url)).headers.get('ETag')
const headers = { 'If-None-Match': etag }
const res2 = await fetch(url, { headers })
expect(res2.status).toBe(304)
})
it('should set Content-Length header', async () => {
const url = `http://localhost:${appPort}`
const res = await fetch(url)
expect(res.headers.get('Content-Length')).toBeDefined()
})
})
})