forked from vercel/next.js
/
index.test.ts
142 lines (129 loc) · 3.46 KB
/
index.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
/* eslint-env jest */
import { remove } from 'fs-extra'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import {
fetchViaHTTP,
findPort,
killApp,
launchApp,
nextBuild,
waitFor,
} from 'next-test-utils'
import { join } from 'path'
jest.setTimeout(1000 * 60 * 2)
const unsupportedFunctions = [
'setImmediate',
'clearImmediate',
// no need to test all of the process methods
'process.cwd',
'process.cpuUsage',
'process.getuid',
]
const undefinedProperties = [
// no need to test all of the process properties
'process.arch',
'process.version',
]
const unsupportedClasses = [
'BroadcastChannel',
'ByteLengthQueuingStrategy',
'CompressionStream',
'CountQueuingStrategy',
'DecompressionStream',
'DomException',
'MessageChannel',
'MessageEvent',
'MessagePort',
'ReadableByteStreamController',
'ReadableStreamBYOBRequest',
'ReadableStreamDefaultController',
'TextDecoderStream',
'TextEncoderStream',
'TransformStreamDefaultController',
'WritableStreamDefaultController',
]
describe.each([
{
title: 'Middleware',
computeRoute(useCase) {
return `/${useCase}`
},
},
{
title: 'Edge route',
computeRoute(useCase) {
return `/api/route?case=${useCase}`
},
},
])('$title using Node.js API', ({ computeRoute }) => {
const appDir = join(__dirname, '..')
describe('dev mode', () => {
let output = ''
let appPort: number
let app = null
beforeAll(async () => {
output = ''
appPort = await findPort()
app = await launchApp(appDir, appPort, {
env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
onStdout(msg) {
output += msg
},
onStderr(msg) {
output += msg
},
})
})
afterAll(() => killApp(app))
it.each(undefinedProperties.map((api) => ({ api })))(
'does not throw on using $api',
async ({ api }) => {
const res = await fetchViaHTTP(appPort, computeRoute(api))
expect(res.status).toBe(200)
await waitFor(500)
expect(output).not.toInclude(`A Node.js API is used (${api})`)
}
)
it.each([
...unsupportedFunctions.map((api) => ({
api,
errorHighlight: `${api}(`,
})),
...unsupportedClasses.map((api) => ({
api,
errorHighlight: `new ${api}(`,
})),
])(`throws error when using $api`, async ({ api, errorHighlight }) => {
const res = await fetchViaHTTP(appPort, computeRoute(api))
expect(res.status).toBe(500)
await waitFor(500)
expect(output)
.toInclude(`A Node.js API is used (${api}) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime`)
expect(stripAnsi(output)).toInclude(errorHighlight)
})
})
describe('production mode', () => {
let buildResult
beforeAll(async () => {
await remove(join(appDir, '.next'))
buildResult = await nextBuild(appDir, undefined, {
stderr: true,
stdout: true,
})
})
it.each(
[...unsupportedFunctions, ...unsupportedClasses].map((api, index) => ({
api,
}))
)(`warns for $api during build`, ({ api }) => {
expect(buildResult.stderr).toContain(`A Node.js API is used (${api}`)
})
it.each(['Buffer', ...undefinedProperties].map((api) => ({ api })))(
'does not warn on using $api',
({ api }) => {
expect(buildResult.stderr).toContain(`A Node.js API is used (${api}`)
}
)
})
})