-
Notifications
You must be signed in to change notification settings - Fork 27.4k
/
Copy pathppr-unstable-cache.test.ts
103 lines (84 loc) · 2.95 KB
/
ppr-unstable-cache.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
import { NextInstance, createNext, isNextDeploy, isNextDev } from 'e2e-utils'
import { findPort } from 'next-test-utils'
import http from 'node:http'
describe('ppr-unstable-cache', () => {
if (isNextDeploy) {
it.skip('should not run in deploy mode', () => {})
return
}
if (isNextDev) {
it.skip('should not run in dev mode', () => {})
return
}
let next: NextInstance | null = null
let server: http.Server | null = null
afterEach(async () => {
if (next) {
await next.destroy()
next = null
}
if (server) {
await server.close()
server = null
}
})
it('should not cache inner fetch calls', async () => {
let generations: string[] = []
server = http.createServer(async (req, res) => {
try {
if (!req.url) throw new Error('No URL')
const cache = new URL(req.url, 'http://n').searchParams.get('cache')
if (!cache) throw new Error('No cache key')
const random = Math.floor(Math.random() * 1000).toString()
const data = cache + ':' + random
generations.push(data)
res.end(data)
} catch (err) {
res.statusCode = 500
res.end(err.message)
}
})
const port = await findPort()
server.listen(port)
next = await createNext({
files: __dirname,
env: { TEST_DATA_SERVER: `http://localhost:${port}/` },
})
expect(generations).toHaveLength(2)
const first = await next
.render$('/')
.then(($) => JSON.parse($('#data').text()))
expect(generations).toHaveLength(2)
expect(first.data.forceCache).toBeOneOf(generations)
expect(first.data.noStore).toBeOneOf(generations)
// Try a few more times, we should always get the same result.
for (let i = 0; i < 3; i++) {
const again = await next
.render$('/')
.then(($) => JSON.parse($('#data').text()))
expect(generations).toHaveLength(2)
expect(first).toEqual(again)
}
// Revalidate the tag associated with the `unstable_cache` call.
const revalidate = await next.fetch('/revalidate-tag', { method: 'POST' })
expect(revalidate.status).toBe(200)
await revalidate.text()
const revalidated = await next
.render$('/')
.then(($) => JSON.parse($('#data').text()))
// Expect that the `cache: no-store` value has been updated, but not
// the `cache: force-cache` value.
expect(generations).toHaveLength(3)
// We know now that the generations have been updated, so let's try to
// validate the value. We don't need to do this within the retry.
expect(revalidated.random).not.toEqual(first.random)
expect(revalidated.data.forceCache).toBe(generations[1])
expect(revalidated.data.noStore).toBe(generations[2])
expect(revalidated).not.toEqual(first)
// Ensure that the `force-cache` value has not been updated, and only called
// once.
expect(generations.filter((g) => g.startsWith('force-cache'))).toHaveLength(
1
)
})
})