-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
index.test.js
180 lines (162 loc) · 5.69 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint-env jest */
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { join } from 'path'
import {
fetchViaHTTP,
findPort,
killApp,
launchApp,
nextBuild,
renderViaHTTP,
waitFor,
} from 'next-test-utils'
const EVAL_ERROR = `Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime`
const DYNAMIC_CODE_ERROR = `Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime`
const WASM_COMPILE_ERROR = `Dynamic WASM code generation (e. g. 'WebAssembly.compile') not allowed in Edge Runtime`
const WASM_INSTANTIATE_ERROR = `Dynamic WASM code generation ('WebAssembly.instantiate' with a buffer parameter) not allowed in Edge Runtime`
jest.setTimeout(1000 * 60 * 2)
const context = {
appDir: join(__dirname, '../'),
}
describe('Page using eval in dev mode', () => {
let output = ''
beforeAll(async () => {
context.appPort = await findPort()
context.app = await launchApp(context.appDir, context.appPort, {
env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
onStdout(msg) {
output += msg
},
onStderr(msg) {
output += msg
},
})
})
beforeEach(() => (output = ''))
afterAll(() => killApp(context.app))
it('does issue dynamic code evaluation warnings', async () => {
const html = await renderViaHTTP(context.appPort, '/')
expect(html).toMatch(/>.*?100.*?and.*?100.*?<\//)
await waitFor(500)
expect(output).not.toContain(EVAL_ERROR)
expect(output).not.toContain(DYNAMIC_CODE_ERROR)
expect(output).not.toContain(WASM_COMPILE_ERROR)
expect(output).not.toContain(WASM_INSTANTIATE_ERROR)
})
})
describe.each([
{
title: 'Middleware',
computeRoute(useCase) {
return `/${useCase}`
},
async extractValue(response) {
return JSON.parse(response.headers.get('data')).value
},
},
{
title: 'Edge route',
computeRoute(useCase) {
return `/api/route?case=${useCase}`
},
async extractValue(response) {
return (await response.json()).value
},
},
])(
'$title usage of dynamic code evaluation',
({ extractValue, computeRoute }) => {
describe('dev mode', () => {
let output = ''
beforeAll(async () => {
context.appPort = await findPort()
context.app = await launchApp(context.appDir, context.appPort, {
env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
onStdout(msg) {
output += msg
},
onStderr(msg) {
output += msg
},
})
})
beforeEach(() => (output = ''))
afterAll(() => killApp(context.app))
it('shows a warning when running code with eval', async () => {
const res = await fetchViaHTTP(
context.appPort,
computeRoute('using-eval')
)
expect(await extractValue(res)).toEqual(100)
await waitFor(500)
expect(output).toContain(EVAL_ERROR)
// TODO check why that has a backslash on windows
expect(output).toMatch(/lib[\\/]utils\.js/)
expect(output).toContain('usingEval')
expect(stripAnsi(output)).toContain("value: eval('100')")
})
it('does not show warning when no code uses eval', async () => {
const res = await fetchViaHTTP(
context.appPort,
computeRoute('not-using-eval')
)
expect(await extractValue(res)).toEqual(100)
await waitFor(500)
expect(output).not.toContain('Dynamic Code Evaluation')
})
it('shows a warning when running WebAssembly.compile', async () => {
const res = await fetchViaHTTP(
context.appPort,
computeRoute('using-webassembly-compile')
)
expect(await extractValue(res)).toEqual(81)
await waitFor(500)
expect(output).toContain(WASM_COMPILE_ERROR)
expect(output).toMatch(/lib[\\/]wasm\.js/)
expect(output).toContain('usingWebAssemblyCompile')
expect(stripAnsi(output)).toContain(
'await WebAssembly.compile(SQUARE_WASM_BUFFER)'
)
})
it('shows a warning when running WebAssembly.instantiate with a buffer parameter', async () => {
const res = await fetchViaHTTP(
context.appPort,
computeRoute('using-webassembly-instantiate-with-buffer')
)
expect(await extractValue(res)).toEqual(81)
await waitFor(500)
expect(output).toContain(WASM_INSTANTIATE_ERROR)
expect(output).toMatch(/lib[\\/]wasm\.js/)
expect(output).toContain('usingWebAssemblyInstantiateWithBuffer')
expect(stripAnsi(output)).toContain(
'await WebAssembly.instantiate(SQUARE_WASM_BUFFER, {})'
)
})
it('does not show a warning when running WebAssembly.instantiate with a module parameter', async () => {
const res = await fetchViaHTTP(
context.appPort,
computeRoute('using-webassembly-instantiate')
)
expect(await extractValue(res)).toEqual(81)
await waitFor(500)
expect(output).not.toContain(WASM_INSTANTIATE_ERROR)
expect(output).not.toContain('DynamicWasmCodeGenerationWarning')
})
})
describe('production mode', () => {
let buildResult
beforeAll(async () => {
buildResult = await nextBuild(context.appDir, undefined, {
stderr: true,
stdout: true,
})
})
it('should have middleware warning during build', () => {
expect(buildResult.stderr).toContain(`Failed to compile`)
expect(buildResult.stderr).toContain(`Used by usingEval, usingEvalSync`)
expect(buildResult.stderr).toContain(`Used by usingWebAssemblyCompile`)
expect(buildResult.stderr).toContain(DYNAMIC_CODE_ERROR)
})
})
}
)