/
index.test.js
128 lines (115 loc) · 3.55 KB
/
index.test.js
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
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
File,
nextBuild,
runDevSuite,
runProdSuite,
fetchViaHTTP,
} from 'next-test-utils'
const appDir = __dirname
const nodeArgs = ['-r', join(appDir, '../../lib/react-17-require-hook.js')]
const reactDomPackagePah = join(appDir, 'node_modules/react-dom')
const nextConfig = new File(join(appDir, 'next.config.js'))
const documentPage = new File(join(appDir, 'pages/_document.js'))
const indexPage = join(appDir, 'pages/index.js')
const indexServerPage = join(appDir, 'pages/index.server.js')
const documentWithGip = `
import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
Document.getInitialProps = (ctx) => {
return ctx.defaultGetInitialProps(ctx)
}
`
function writeNextConfig(config, reactVersion = 17) {
const content = `
const path = require('path')
const withReact = ${reactVersion} === 18 ? v => v : require(path.join(__dirname, '../../lib/with-react-17.js'))
module.exports = withReact({ experimental: ${JSON.stringify(config)} })
`
nextConfig.write(content)
}
describe('Invalid react 18 webpack config', () => {
it('should enable `experimental.reactRoot` when `experimental.runtime` is enabled', async () => {
writeNextConfig({
runtime: 'edge',
})
const { stderr } = await nextBuild(appDir, [], { stderr: true, nodeArgs })
nextConfig.restore()
expect(stderr).toContain(
'`experimental.runtime` requires `experimental.reactRoot` to be enabled along with React 18.'
)
})
})
describe('React 17 with React 18 config', () => {
beforeAll(async () => {
await fs.mkdirp(reactDomPackagePah)
await fs.writeFile(
join(reactDomPackagePah, 'package.json'),
JSON.stringify({ name: 'react-dom', version: '17.0.0' })
)
writeNextConfig({ reactRoot: true })
})
afterAll(async () => {
await fs.remove(reactDomPackagePah)
nextConfig.restore()
})
it('should warn user when not using react 18 and `experimental.reactRoot` is enabled', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true, nodeArgs })
expect(stderr).toContain(
'You have to use React 18 to use `experimental.reactRoot`.'
)
})
it('suspense is not allowed in blocking rendering mode', async () => {
const { stderr, code } = await nextBuild(appDir, [], {
stderr: true,
nodeArgs,
})
expect(stderr).toContain(
'Invalid suspense option usage in next/dynamic. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense'
)
expect(code).toBe(1)
})
})
const documentSuite = {
runTests: (context, env) => {
if (env === 'dev') {
it('should error when custom _document has getInitialProps method', async () => {
const res = await fetchViaHTTP(context.appPort, '/')
expect(res.status).toBe(500)
})
} else {
it('should failed building', async () => {
expect(context.code).toBe(1)
})
}
},
beforeAll: async () => {
writeNextConfig(
{
serverComponents: true,
},
18
)
documentPage.write(documentWithGip)
await fs.rename(indexPage, indexServerPage)
},
afterAll: async () => {
documentPage.delete()
nextConfig.restore()
await fs.rename(indexServerPage, indexPage)
},
}
runDevSuite('Invalid custom document with gip', appDir, documentSuite)
runProdSuite('Invalid custom document with gip', appDir, documentSuite)