forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule-layer.test.ts
More file actions
131 lines (115 loc) · 4.4 KB
/
module-layer.test.ts
File metadata and controls
131 lines (115 loc) · 4.4 KB
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
import { nextTestSetup } from 'e2e-utils'
import { assertHasRedbox, getRedboxSource, retry } from 'next-test-utils'
describe('module layer', () => {
const { next, isNextStart, isNextDev } = nextTestSetup({
files: __dirname,
})
function runTests() {
const routes = [
// app client components pages
'/app/client',
'/app/client-edge',
// app sever components pages
'/app/server',
'/app/server-edge',
// app routes
'/app/route',
'/app/route-edge',
// pages/api
'/api/default',
'/api/default-edge',
'/api/server-only',
'/api/server-only-edge',
'/api/mixed',
]
for (const route of routes) {
it(`should render routes marked with restriction marks without errors ${route}`, async () => {
const { status } = await next.fetch(route)
expect([route, status]).toEqual([route, 200])
})
}
it('should render installed react-server condition for middleware', async () => {
const json = await next.fetch('/middleware').then((res) => res.json())
expect(json.React).toContain('version') // basic react-server export
expect(json.React).not.toContain('useEffect') // no client api export
})
// This is for backward compatibility, don't change react usage in existing pages/api
it('should contain client react exports for pages api', async () => {
async function verifyReactExports(route, isEdge) {
const json = await next.fetch(route).then((res) => res.json())
// contain all react-server and default condition exports
expect(json.React).toContain('version')
expect(json.React).toContain('useEffect')
// contain react-dom-server default condition exports
expect(json.ReactDomServer).toContain('version')
expect(json.ReactDomServer).toContain('renderToString')
expect(json.ReactDomServer).toContain('renderToStaticMarkup')
expect(json.ReactDomServer).toContain(
isEdge ? 'renderToReadableStream' : 'renderToPipeableStream'
)
}
await verifyReactExports('/api/default', false)
await verifyReactExports('/api/default-edge', true)
await verifyReactExports('/api/server-only', false)
await verifyReactExports('/api/server-only-edge', true)
})
if (isNextStart) {
it('should log the build info properly', async () => {
const cliOutput = next.cliOutput
expect(cliOutput).toContain('Middleware')
const functionsManifest = JSON.parse(
await next.readFile('.next/server/functions-config-manifest.json')
)
expect(functionsManifest.functions).toContainKeys([
'/app/route-edge',
'/api/default-edge',
'/api/server-only-edge',
'/app/client-edge',
'/app/server-edge',
])
const pagesManifest = JSON.parse(
await next.readFile('.next/server/pages-manifest.json')
)
const middlewareManifest = JSON.parse(
await next.readFile('.next/server/middleware-manifest.json')
)
expect(middlewareManifest.middleware).toBeTruthy()
expect(pagesManifest).toContainKeys([
'/api/default-edge',
'/pages-ssr',
'/api/default',
'/api/server-only',
])
})
}
}
if (isNextDev) {
describe('client packages in middleware', () => {
const middlewareFile = 'middleware.js'
let middlewareContent = ''
afterAll(async () => {
await next.patchFile(middlewareFile, middlewareContent)
})
it('should error when import server packages in middleware', async () => {
const browser = await next.browser('/')
middlewareContent = await next.readFile(middlewareFile)
await next.patchFile(
middlewareFile,
middlewareContent
.replace("import 'server-only'", "// import 'server-only'")
.replace("// import './lib/mixed-lib'", "import './lib/mixed-lib'")
)
await retry(async () => {
await assertHasRedbox(browser)
const source = await getRedboxSource(browser)
expect(source).toContain(
`You're importing a component that imports client-only. It only works in a Client Component but none of its parents are marked with "use client"`
)
})
})
})
}
describe('with server-only in server targets', () => {
runTests()
})
})