forked from vercel/next.js
/
rsc-errors.test.ts
121 lines (108 loc) · 3.76 KB
/
rsc-errors.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
import path from 'path'
import { check, fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
describe('app dir - rsc errors', () => {
let next: NextInstance
const { isNextDeploy, isNextDev } = global as any
const isReact17 = process.env.NEXT_TEST_REACT_VERSION === '^17'
if (isNextDeploy || isReact17) {
it('should skip tests for next-deploy and react 17', () => {})
return
}
if (!isNextDev) {
it('should skip tests for next-start', () => {})
return
}
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, './rsc-errors')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
},
})
})
afterAll(() => next.destroy())
it('should throw an error when getServerSideProps is used', async () => {
const pageFile = 'app/client-with-errors/get-server-side-props/page.js'
const content = await next.readFile(pageFile)
const uncomment = content.replace(
'// export function getServerSideProps',
'export function getServerSideProps'
)
await next.patchFile(pageFile, uncomment)
const res = await fetchViaHTTP(
next.url,
'/client-with-errors/get-server-side-props'
)
await next.patchFile(pageFile, content)
await check(async () => {
const { status } = await fetchViaHTTP(
next.url,
'/client-with-errors/get-server-side-props'
)
return status
}, /200/)
expect(res.status).toBe(500)
expect(await res.text()).toContain(
'"getServerSideProps\\" is not supported in app/'
)
})
it('should throw an error when getStaticProps is used', async () => {
const pageFile = 'app/client-with-errors/get-static-props/page.js'
const content = await next.readFile(pageFile)
const uncomment = content.replace(
'// export function getStaticProps',
'export function getStaticProps'
)
await next.patchFile(pageFile, uncomment)
const res = await fetchViaHTTP(
next.url,
'/client-with-errors/get-static-props'
)
await next.patchFile(pageFile, content)
await check(async () => {
const { status } = await fetchViaHTTP(
next.url,
'/client-with-errors/get-static-props'
)
return status
}, /200/)
expect(res.status).toBe(500)
expect(await res.text()).toContain(
'"getStaticProps\\" is not supported in app/'
)
})
it('should error for styled-jsx imports on server side', async () => {
const html = await renderViaHTTP(next.url, '/server-with-errors/styled-jsx')
expect(html).toContain(
'This module cannot be imported from a Server Component module. It should only be used from a Client Component.'
)
})
it('should error when page component export is not valid', async () => {
const html = await renderViaHTTP(
next.url,
'/server-with-errors/page-export'
)
expect(html).toContain(
'The default export is not a React Component in page:'
)
})
it('should throw an error when "use client" is on the top level but after other expressions', async () => {
const pageFile = 'app/swc/use-client/page.js'
const content = await next.readFile(pageFile)
const uncomment = content.replace("// 'use client'", "'use client'")
await next.patchFile(pageFile, uncomment)
const res = await fetchViaHTTP(next.url, '/swc/use-client')
await next.patchFile(pageFile, content)
await check(async () => {
const { status } = await fetchViaHTTP(next.url, '/swc/use-client')
return status
}, /200/)
expect(res.status).toBe(500)
expect(await res.text()).toContain(
'directive must be placed before other expressions'
)
})
})