-
Notifications
You must be signed in to change notification settings - Fork 28.3k
/
Copy pathtestmode.test.ts
137 lines (116 loc) · 4.26 KB
/
testmode.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
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
import { nextTestSetup } from 'e2e-utils'
import { createProxyServer } from 'next/experimental/testmode/proxy'
describe('testmode', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
dependencies: require('./package.json').dependencies,
})
if (skipped) {
return
}
let proxyServer: Awaited<ReturnType<typeof createProxyServer>>
beforeEach(async () => {
proxyServer = await createProxyServer({
onFetch: async (testData, request) => {
if (
request.method === 'GET' &&
[
'https://example.com/',
'https://next-data-api-endpoint.vercel.app/api/random',
].includes(request.url)
) {
return new Response(testData)
}
if (
request.method === 'GET' &&
request.url === 'https://example.com/middleware'
) {
return new Response(`middleware-${testData}`)
}
return undefined
},
})
})
afterEach(async () => {
proxyServer.close()
})
const fetchForTest = async (url: string, testData?: string) => {
return next.fetch(url, {
headers: {
'Next-Test-Proxy-Port': String(proxyServer.port),
'Next-Test-Data': testData ?? 'test1',
},
})
}
describe('app router', () => {
it('should fetch real data when Next-Test-* headers are not present', async () => {
const html = await (await next.fetch('/app/rsc-fetch')).text()
expect(html).not.toContain('<pre>test1</pre>')
})
it('should handle RSC with fetch in serverless function', async () => {
const html = await (await fetchForTest('/app/rsc-fetch')).text()
expect(html).toContain('<pre>test1</pre>')
})
it('should avoid fetch cache', async () => {
const html1 = await (await fetchForTest('/app/rsc-fetch')).text()
expect(html1).toContain('<pre>test1</pre>')
const html2 = await (await fetchForTest('/app/rsc-fetch', 'test2')).text()
expect(html2).toContain('<pre>test2</pre>')
})
it('should handle RSC with http.get in serverless function', async () => {
const html = await (await fetchForTest('/app/rsc-httpget')).text()
expect(html).toContain('<pre>test1</pre>')
})
it('should handle RSC with fetch in edge function', async () => {
const html = await (await fetchForTest('/app/rsc-fetch-edge')).text()
expect(html).toContain('<pre>test1</pre>')
})
it('should handle API with fetch in serverless function', async () => {
const json = await (await fetchForTest('/api/fetch')).json()
expect(json.text).toEqual('test1')
})
it('should handle API with http.get in serverless function', async () => {
const json = await (await fetchForTest('/api/httpget')).json()
expect(json.text).toEqual('test1')
})
it('should handle API with fetch in edge function', async () => {
const json = await (await fetchForTest('/api/fetch-edge')).json()
expect(json.text).toEqual('test1')
})
})
describe('page router', () => {
it('should handle getServerSideProps with fetch', async () => {
const html = await (
await fetchForTest('/pages/getServerSidePropsFetch')
).text()
expect(html).toContain('<pre>test1</pre>')
})
it('should handle getServerSideProps with http.get', async () => {
const html = await (
await fetchForTest('/pages/getServerSidePropsHttpGet')
).text()
expect(html).toContain('<pre>test1</pre>')
})
it('should handle API with fetch', async () => {
const json = await (await fetchForTest('/api/pages/fetch')).json()
expect(json.text).toEqual('test1')
})
it('should handle API with http.get', async () => {
const json = await (await fetchForTest('/api/pages/httpget')).json()
expect(json.text).toEqual('test1')
})
})
describe('middleware', () => {
it('should intercept fetchs in middleware', async () => {
const resp = await fetchForTest('/app/rsc-fetch')
expect(resp.headers.get('x-middleware-fetch')).toEqual('middleware-test1')
})
})
describe('rewrites', () => {
it('should handle rewrites', async () => {
const text = await (await fetchForTest('/rewrite-1')).text()
expect(text).toEqual('test1')
})
})
})