-
Notifications
You must be signed in to change notification settings - Fork 26k
/
index.test.js
125 lines (111 loc) · 3.16 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
/* eslint-env jest */
import { join } from 'path'
import {
File,
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
renderViaHTTP,
hasRedbox,
getRedboxHeader,
} from 'next-test-utils'
import concurrent from './concurrent'
import basics from './basics'
import strictMode from './strict-mode'
import webdriver from 'next-webdriver'
// overrides react and react-dom to v18
const nodeArgs = ['-r', join(__dirname, 'require-hook.js')]
const appDir = join(__dirname, '../app')
const nextConfig = new File(join(appDir, 'next.config.js'))
const invalidPage = new File(join(appDir, 'pages/invalid.js'))
describe('Basics', () => {
runTests('default setting with react 18', (context, env) =>
basics(context, env)
)
})
// React 18 with Strict Mode enabled might cause double invocation of lifecycle methods.
describe('Strict mode - dev', () => {
const context = { appDir }
beforeAll(async () => {
nextConfig.replace('// reactStrictMode: true,', 'reactStrictMode: true,')
context.appPort = await findPort()
context.server = await launchApp(context.appDir, context.appPort, {
nodeArgs,
})
})
afterAll(() => {
nextConfig.restore()
killApp(context.server)
})
strictMode(context)
})
function runTestsAgainstRuntime(runtime) {
runTests(
`Concurrent mode in the ${runtime} runtime`,
(context, env) => {
concurrent(context, (p, q) => renderViaHTTP(context.appPort, p, q))
if (env === 'dev') {
it('should recover after undefined exported as default', async () => {
const browser = await webdriver(context.appPort, '/invalid')
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toMatch(
`Error: The default export is not a React Component in page: "/invalid"`
)
})
}
},
{
beforeAll: (env) => {
if (env === 'dev') {
invalidPage.write(`export const value = 1`)
}
nextConfig.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
},
afterAll: (env) => {
if (env === 'dev') {
invalidPage.delete()
}
nextConfig.restore()
},
}
)
}
function runTest(env, name, fn, options) {
const context = { appDir }
describe(`${name} (${env})`, () => {
beforeAll(async () => {
context.appPort = await findPort()
context.stderr = ''
options?.beforeAll(env)
if (env === 'dev') {
context.server = await launchApp(context.appDir, context.appPort, {
nodeArgs,
onStderr(msg) {
context.stderr += msg
},
})
} else {
await nextBuild(context.appDir, [], { nodeArgs })
context.server = await nextStart(context.appDir, context.appPort, {
nodeArgs,
onStderr(msg) {
context.stderr += msg
},
})
}
})
afterAll(async () => {
options?.afterAll(env)
await killApp(context.server)
})
fn(context, env)
})
}
runTestsAgainstRuntime('edge')
runTestsAgainstRuntime('nodejs')
function runTests(name, fn, options) {
runTest('dev', name, fn, options)
runTest('prod', name, fn, options)
}