/
index.test.js
101 lines (94 loc) · 2.84 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
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import {
renderViaHTTP,
findPort,
nextBuild,
nextStart,
killApp,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
const static404 = join(appDir, '.next/server/static/test-id/pages/404.html')
const appPage = join(appDir, 'pages/_app.js')
const errorPage = join(appDir, 'pages/_error.js')
const buildId = `generateBuildId: () => 'test-id'`
let app
let appPort
describe('Static 404 page', () => {
afterEach(async () => {
await fs.remove(appPage)
await fs.remove(errorPage)
await fs.remove(nextConfig)
})
beforeEach(() => fs.remove(join(appDir, '.next/server')))
describe('With config enabled', () => {
beforeEach(() =>
fs.writeFile(nextConfig, `module.exports = { ${buildId} }`)
)
it('should export 404 page without custom _error', async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
const html = await renderViaHTTP(appPort, '/non-existent')
await killApp(app)
expect(html).toContain('This page could not be found')
expect(await fs.exists(static404)).toBe(true)
})
it('should export 404 page without custom _error (serverless)', async () => {
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'experimental-serverless-trace'
}
`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
const html = await renderViaHTTP(appPort, '/non-existent')
await killApp(app)
expect(html).toContain('This page could not be found')
expect(
await fs.exists(join(appDir, '.next/serverless/pages/404.html'))
).toBe(true)
})
it('should not export 404 page with custom _error GIP', async () => {
await fs.writeFile(
errorPage,
`
import Error from 'next/error'
export default class MyError extends Error {
static getInitialProps() {
return {
statusCode: 404
}
}
}
`
)
await nextBuild(appDir)
await fs.remove(errorPage)
expect(await fs.exists(static404)).toBe(false)
})
it('should not export 404 page with getInitialProps in _app', async () => {
await fs.writeFile(
appPage,
`
const Page = ({ Component, pageProps }) => {
return <Component {...pageProps} />
}
Page.getInitialProps = () => ({ hello: 'world', pageProps: {} })
export default Page
`
)
await nextBuild(appDir)
await fs.remove(appPage)
expect(await fs.exists(static404)).toBe(false)
})
})
})