-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathindex.test.ts
135 lines (125 loc) · 4.74 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
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
import { fetchViaHTTP, File, nextBuild } from 'next-test-utils'
import { join } from 'path'
import stripAnsi from 'strip-ansi'
const pagePath = 'pages/index.jsx'
const apiPath = 'pages/api/edge.js'
;(process.env.TURBOPACK ? describe.skip.each : describe.each)([
{ appDir: join(__dirname, './app/src'), title: 'src/pages and API routes' },
{ appDir: join(__dirname, './app'), title: 'pages and API routes' },
])('Configurable runtime for $title', ({ appDir }) => {
let next: NextInstance
const page = new File(join(appDir, pagePath))
const api = new File(join(appDir, apiPath))
if ((global as any).isNextDev) {
describe('In development mode', () => {
beforeAll(async () => {
next = await createNext({
files: new FileRef(appDir),
dependencies: {},
skipStart: true,
})
})
afterEach(async () => {
await next.stop()
await next.patchFile(pagePath, page.originalContent)
await next.patchFile(apiPath, api.originalContent)
})
afterAll(() => next.destroy())
it('runs with no warning API route on the edge runtime', async () => {
await next.start()
const res = await fetchViaHTTP(next.url, `/api/edge`)
expect(res.status).toEqual(200)
expect(next.cliOutput).not.toInclude('error')
expect(next.cliOutput).not.toInclude('warn')
})
it('warns about API route using experimental-edge runtime', async () => {
await next.patchFile(
apiPath,
`
export default () => new Response('ok');
export const config = { runtime: 'experimental-edge' };
`
)
await next.start()
const res = await fetchViaHTTP(next.url, `/api/edge`)
expect(res.status).toEqual(200)
expect(next.cliOutput).not.toInclude('error')
expect(stripAnsi(next.cliOutput)).toInclude(
`/api/edge provided runtime 'experimental-edge'. It can be updated to 'edge' instead.`
)
})
it('warns about page using edge runtime', async () => {
await next.patchFile(
pagePath,
`
export default () => (<p>hello world</p>);
export const runtime = 'experimental-edge';
`
)
await next.start()
const res = await fetchViaHTTP(next.url, `/`)
expect(res.status).toEqual(200)
expect(next.cliOutput).not.toInclude('error')
expect(stripAnsi(next.cliOutput)).toInclude(
`You are using an experimental edge runtime, the API might change.`
)
})
it('errors about page using edge runtime', async () => {
await next.patchFile(
pagePath,
`
export default () => (<p>hello world</p>);
export const runtime = 'edge';
`
)
await next.start()
const res = await fetchViaHTTP(next.url, `/`)
expect(res.status).toEqual(200)
expect(stripAnsi(next.cliOutput)).toInclude(
`Page / provided runtime 'edge', the edge runtime for rendering is currently experimental. Use runtime 'experimental-edge' instead.`
)
expect(next.cliOutput).not.toInclude('warn')
})
})
} else if ((global as any).isNextStart) {
describe('In start mode', () => {
// TODO because createNext runs process.exit() without any log info on build failure, rely on good old nextBuild()
afterEach(async () => {
page.restore()
api.restore()
})
it('builds with API route on the edge runtime and page on the experimental edge runtime', async () => {
page.write(`
export default () => (<p>hello world</p>);
export const runtime = 'experimental-edge';
`)
const output = await nextBuild(appDir, undefined, {
stdout: true,
stderr: true,
})
expect(output.code).toBe(0)
expect(output.stderr).not.toContain(`error`)
expect(output.stdout).not.toContain(`warn`)
})
it('does not build with page on the edge runtime', async () => {
page.write(`
export default () => (<p>hello world</p>);
export const runtime = 'edge';
`)
const output = await nextBuild(appDir, undefined, {
stdout: true,
stderr: true,
})
expect(output.code).toBe(1)
expect(output.stderr).not.toContain(`Build failed`)
expect(stripAnsi(output.stderr)).toContain(
`Error: Page / provided runtime 'edge', the edge runtime for rendering is currently experimental. Use runtime 'experimental-edge' instead.`
)
})
})
} else {
it.skip('no deploy tests', () => {})
}
})