-
Notifications
You must be signed in to change notification settings - Fork 26k
/
index.test.js
103 lines (89 loc) · 2.72 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
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
renderViaHTTP,
findPort,
nextBuild,
nextStart,
killApp,
launchApp,
} from 'next-test-utils'
const appDir = join(__dirname, '..')
const page404 = join(appDir, 'pages/404.js')
let appPort
let app
const runTests = () => {
it('renders custom _error successfully', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Custom error/)
})
}
const customErrNo404Match =
/You have added a custom \/_error page without a custom \/404 page/
describe('Custom _error', () => {
describe('dev mode 1', () => {
let stderr = ''
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStderr(msg) {
stderr += msg || ''
},
})
})
afterAll(() => killApp(app))
it('should not warn with /_error and /404 when rendering error first', async () => {
stderr = ''
await fs.writeFile(page404, 'export default <h1>')
const html = await renderViaHTTP(appPort, '/404')
await fs.remove(page404)
expect(html).toContain('Unexpected eof')
expect(stderr).not.toMatch(customErrNo404Match)
})
})
describe('dev mode 2', () => {
let stderr = ''
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStderr(msg) {
stderr += msg || ''
},
})
})
afterAll(() => killApp(app))
it('should not warn with /_error and /404', async () => {
stderr = ''
await fs.writeFile(page404, `export default () => 'not found...'`)
const html = await renderViaHTTP(appPort, '/404')
await fs.remove(page404)
expect(html).toContain('not found...')
expect(stderr).not.toMatch(customErrNo404Match)
})
it('should warn on custom /_error without custom /404', async () => {
stderr = ''
const html = await renderViaHTTP(appPort, '/404')
expect(stderr).toMatch(customErrNo404Match)
expect(html).toContain('An error 404 occurred on server')
})
})
;(process.env.TURBOPACK ? describe.skip : describe)('production mode', () => {
let buildOutput = ''
beforeAll(async () => {
const { stdout, stderr } = await nextBuild(appDir, undefined, {
stdout: true,
stderr: true,
})
buildOutput = (stdout || '') + (stderr || '')
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
it('should not contain /_error in build output', async () => {
expect(buildOutput).toMatch(/λ .*?\/404/)
expect(buildOutput).not.toMatch(/λ .*?\/_error/)
})
runTests()
})
})