forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
129 lines (110 loc) · 3.76 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
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import {
findPort,
killApp,
nextBuild,
nextStart,
renderViaHTTP,
waitFor,
} from 'next-test-utils'
import { join } from 'path'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let app
let appPort
let buildId
let stderr
function runTests(route, routePath, serverless) {
it(`[${route}] should not revalidate when set to false`, async () => {
const fileName = join(
appDir,
`.next`,
...(serverless ? ['serverless'] : ['server', 'static', buildId]),
`pages/${routePath}.html`
)
const initialHtml = await renderViaHTTP(appPort, route)
const initialFileHtml = await fs.readFile(fileName, 'utf8')
let newHtml = await renderViaHTTP(appPort, route)
expect(initialHtml).toBe(newHtml)
expect(await fs.readFile(fileName, 'utf8')).toBe(initialFileHtml)
await waitFor(500)
newHtml = await renderViaHTTP(appPort, route)
expect(initialHtml).toBe(newHtml)
expect(await fs.readFile(fileName, 'utf8')).toBe(initialFileHtml)
await waitFor(500)
newHtml = await renderViaHTTP(appPort, route)
expect(initialHtml).toBe(newHtml)
expect(await fs.readFile(fileName, 'utf8')).toBe(initialFileHtml)
expect(stderr).not.toContain('GSP was re-run')
})
it(`[${route}] should not revalidate /_next/data when set to false`, async () => {
const route = join(`/_next/data/${buildId}`, `${routePath}.json`)
const fileName = join(
appDir,
`.next`,
...(serverless ? ['serverless'] : ['server', 'static', buildId]),
`pages/${routePath}.json`
)
const initialData = JSON.parse(await renderViaHTTP(appPort, route))
const initialFileJson = await fs.readFile(fileName, 'utf8')
expect(JSON.parse(await renderViaHTTP(appPort, route))).toEqual(initialData)
expect(await fs.readFile(fileName, 'utf8')).toBe(initialFileJson)
await waitFor(500)
expect(JSON.parse(await renderViaHTTP(appPort, route))).toEqual(initialData)
expect(await fs.readFile(fileName, 'utf8')).toBe(initialFileJson)
await waitFor(500)
expect(JSON.parse(await renderViaHTTP(appPort, route))).toEqual(initialData)
expect(await fs.readFile(fileName, 'utf8')).toBe(initialFileJson)
expect(stderr).not.toContain('GSP was re-run')
})
}
describe('SSG Prerender No Revalidate', () => {
afterAll(() => fs.remove(nextConfig))
describe('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'experimental-serverless-trace' }`,
'utf8'
)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
stderr = ''
app = await nextStart(appDir, appPort, {
onStderr: msg => {
stderr += msg
},
})
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(() => killApp(app))
runTests('/', '/index', true)
runTests('/named', '/named', true)
runTests('/nested', '/nested', true)
runTests('/nested/named', '/nested/named', true)
})
describe('production mode', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir, [])
appPort = await findPort()
stderr = ''
app = await nextStart(appDir, appPort, {
onStderr: msg => {
stderr += msg
},
})
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(() => killApp(app))
runTests('/', '/index')
runTests('/named', '/named')
runTests('/nested', '/nested')
runTests('/nested/named', '/nested/named')
})
})