forked from vercel/next.js
/
server-components.test.ts
160 lines (145 loc) · 4.38 KB
/
server-components.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* eslint-env jest */
import { sandbox } from './helpers'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import path from 'path'
describe('Error Overlay for server components', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
},
skipStart: true,
})
})
afterAll(() => next.destroy())
describe('createContext called in Server Component', () => {
it('should show error when React.createContext is called', async () => {
const { session, browser, cleanup } = await sandbox(
next,
new Map([
[
'app/page.js',
`
import React from 'react'
const Context = React.createContext()
export default function Page() {
return (
<>
<Context.Provider value="hello">
<h1>Page</h1>
</Context.Provider>
</>
)
}`,
],
])
)
// TODO-APP: currently requires a full reload because moving from a client component to a server component isn't causing a Fast Refresh yet.
await browser.refresh()
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource(true)).toMatchSnapshot()
expect(next.cliOutput).toContain(
'createContext only works in Client Components'
)
await cleanup()
})
it('should show error when React.createContext is called in external package', async () => {
const { session, browser, cleanup } = await sandbox(
next,
new Map([
[
'node_modules/my-package/index.js',
`
const React = require('react')
module.exports = React.createContext()
`,
],
[
'node_modules/my-package/package.json',
`
{
"name": "my-package",
"version": "0.0.1"
}
`,
],
[
'app/page.js',
`
import Context from 'my-package'
export default function Page() {
return (
<>
<Context.Provider value="hello">
<h1>Page</h1>
</Context.Provider>
</>
)
}`,
],
])
)
// TODO-APP: currently requires a full reload because moving from a client component to a server component isn't causing a Fast Refresh yet.
await browser.refresh()
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource(true)).toMatchSnapshot()
expect(next.cliOutput).toContain(
'createContext only works in Client Components'
)
await cleanup()
})
it('should show error when createContext is called in external package', async () => {
const { session, browser, cleanup } = await sandbox(
next,
new Map([
[
'node_modules/my-package/index.js',
`
const { createContext } = require('react')
module.exports = createContext()
`,
],
[
'node_modules/my-package/package.json',
`
{
"name": "my-package",
"version": "0.0.1"
}
`,
],
[
'app/page.js',
`
import Context from 'my-package'
export default function Page() {
return (
<>
<Context.Provider value="hello">
<h1>Page</h1>
</Context.Provider>
</>
)
}`,
],
])
)
// TODO-APP: currently requires a full reload because moving from a client component to a server component isn't causing a Fast Refresh yet.
await browser.refresh()
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource(true)).toMatchSnapshot()
expect(next.cliOutput).toContain(
'createContext only works in Client Components'
)
await cleanup()
})
})
})