forked from vercel/next.js
/
index.test.js
213 lines (178 loc) · 6.72 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
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import {
nextBuild,
nextStart,
findPort,
killApp,
fetchViaHTTP,
waitFor,
check,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '..')
const dataFile = join(appDir, 'data.txt')
let app
let appPort
const runTests = () => {
it('should revalidate page when notFund returned during build', async () => {
let res = await fetchViaHTTP(appPort, '/initial-not-found/first')
let $ = cheerio.load(await res.text())
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
res = await fetchViaHTTP(appPort, '/initial-not-found/second')
$ = cheerio.load(await res.text())
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
res = await fetchViaHTTP(appPort, '/initial-not-found')
$ = cheerio.load(await res.text())
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
await fs.writeFile(dataFile, '200')
// wait for revalidation period
await waitFor(1500)
await fetchViaHTTP(appPort, '/initial-not-found/first')
await fetchViaHTTP(appPort, '/initial-not-found/second')
await fetchViaHTTP(appPort, '/initial-not-found')
// wait for revalidation to occur in background
try {
await check(async () => {
res = await fetchViaHTTP(appPort, '/initial-not-found/first')
$ = cheerio.load(await res.text())
return res.status === 200 && $('#data').text() === '200'
? 'success'
: `${res.status} - ${$('#data').text()}`
}, 'success')
await check(async () => {
res = await fetchViaHTTP(appPort, '/initial-not-found/second')
$ = cheerio.load(await res.text())
return res.status === 200 && $('#data').text() === '200'
? 'success'
: `${res.status} - ${$('#data').text()}`
}, 'success')
await check(async () => {
res = await fetchViaHTTP(appPort, '/initial-not-found')
$ = cheerio.load(await res.text())
return res.status === 200 && $('#data').text() === '200'
? 'success'
: `${res.status} - ${$('#data').text()}`
}, 'success')
} finally {
await fs.writeFile(dataFile, '404')
}
})
it('should revalidate after notFound is returned for fallback: blocking', async () => {
let res = await fetchViaHTTP(appPort, '/fallback-blocking/hello')
let $ = cheerio.load(await res.text())
const privateCache =
'private, no-cache, no-store, max-age=0, must-revalidate'
expect(res.headers.get('cache-control')).toBe(privateCache)
expect(res.status).toBe(404)
expect(JSON.parse($('#props').text()).notFound).toBe(true)
await waitFor(1000)
res = await fetchViaHTTP(appPort, '/fallback-blocking/hello')
$ = cheerio.load(await res.text())
expect(res.headers.get('cache-control')).toBe(privateCache)
expect(res.status).toBe(404)
expect(JSON.parse($('#props').text()).notFound).toBe(true)
await waitFor(1000)
res = await fetchViaHTTP(appPort, '/fallback-blocking/hello')
$ = cheerio.load(await res.text())
const props = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
expect(res.status).toBe(200)
expect(props.found).toBe(true)
expect(props.params).toEqual({ slug: 'hello' })
expect(isNaN(props.random)).toBe(false)
await waitFor(1000)
res = await fetchViaHTTP(appPort, '/fallback-blocking/hello')
$ = cheerio.load(await res.text())
const props2 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
expect(res.status).toBe(200)
expect(props2.found).toBe(true)
expect(props2.params).toEqual({ slug: 'hello' })
expect(isNaN(props2.random)).toBe(false)
await waitFor(1000)
res = await fetchViaHTTP(appPort, '/fallback-blocking/hello')
$ = cheerio.load(await res.text())
const props3 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
expect(res.status).toBe(200)
expect(props3.found).toBe(true)
expect(props3.params).toEqual({ slug: 'hello' })
expect(isNaN(props3.random)).toBe(false)
expect(props3.random).not.toBe(props.random)
})
it('should revalidate after notFound is returned for fallback: true', async () => {
const browser = await webdriver(appPort, '/fallback-true/world')
await browser.waitForElementByCss('#not-found')
await waitFor(1000)
let res = await fetchViaHTTP(appPort, '/fallback-true/world')
let $ = cheerio.load(await res.text())
expect(res.headers.get('cache-control')).toBe(
'private, no-cache, no-store, max-age=0, must-revalidate'
)
expect(res.status).toBe(404)
expect(JSON.parse($('#props').text()).notFound).toBe(true)
await waitFor(1000)
res = await fetchViaHTTP(appPort, '/fallback-true/world')
$ = cheerio.load(await res.text())
const props = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
expect(res.status).toBe(200)
expect(props.found).toBe(true)
expect(props.params).toEqual({ slug: 'world' })
expect(isNaN(props.random)).toBe(false)
await waitFor(1000)
res = await fetchViaHTTP(appPort, '/fallback-true/world')
$ = cheerio.load(await res.text())
const props2 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
expect(res.status).toBe(200)
expect(props2.found).toBe(true)
expect(props2.params).toEqual({ slug: 'world' })
expect(isNaN(props2.random)).toBe(false)
await waitFor(1000)
res = await fetchViaHTTP(appPort, '/fallback-true/world')
$ = cheerio.load(await res.text())
const props3 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
expect(res.status).toBe(200)
expect(props3.found).toBe(true)
expect(props3.params).toEqual({ slug: 'world' })
expect(isNaN(props3.random)).toBe(false)
expect(props3.random).not.toBe(props.random)
})
}
describe('SSG notFound revalidate', () => {
describe('production mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir, undefined, {
cwd: appDir,
})
appPort = await findPort()
app = await nextStart(appDir, appPort, {
cwd: appDir,
})
})
afterAll(() => killApp(app))
runTests()
})
})