forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
125 lines (103 loc) · 3.38 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
/* eslint-env jest */
import { join } from 'path'
import {
nextBuild,
nextExport,
startStaticServer,
launchApp,
stopApp,
killApp,
findPort,
renderViaHTTP,
File,
} from 'next-test-utils'
import ssr from './ssr'
import browser from './browser'
import dev from './dev'
import { promises } from 'fs'
import dynamic from './dynamic'
import apiRoutes from './api-routes'
const { access, mkdir, writeFile, stat } = promises
const appDir = join(__dirname, '../')
const outdir = join(appDir, 'out')
const outNoTrailSlash = join(appDir, 'outNoTrailSlash')
const context = {}
context.appDir = appDir
const devContext = {}
const nextConfig = new File(join(appDir, 'next.config.js'))
const fileExist = (path) =>
access(path)
.then(() => stat(path))
.then((stats) => (stats.isFile() ? true : false))
.catch(() => false)
describe('Static Export', () => {
it('should delete existing exported files', async () => {
const tempfile = join(outdir, 'temp.txt')
await mkdir(outdir).catch((e) => {
if (e.code !== 'EEXIST') throw e
})
await writeFile(tempfile, 'Hello there')
await nextBuild(appDir)
await nextExport(appDir, { outdir })
let doesNotExist = false
await access(tempfile).catch((e) => {
if (e.code === 'ENOENT') doesNotExist = true
})
expect(doesNotExist).toBe(true)
})
beforeAll(async () => {
await nextBuild(appDir)
await nextExport(appDir, { outdir })
nextConfig.replace(
`exportTrailingSlash: true`,
`exportTrailingSlash: false`
)
await nextBuild(appDir)
await nextExport(appDir, { outdir: outNoTrailSlash })
nextConfig.restore()
context.server = await startStaticServer(outdir)
context.port = context.server.address().port
context.serverNoTrailSlash = await startStaticServer(outNoTrailSlash)
context.portNoTrailSlash = context.serverNoTrailSlash.address().port
devContext.port = await findPort()
devContext.server = await launchApp(
join(__dirname, '../'),
devContext.port,
true
)
// pre-build all pages at the start
await Promise.all([
renderViaHTTP(devContext.port, '/'),
renderViaHTTP(devContext.port, '/dynamic/one'),
])
})
afterAll(async () => {
await Promise.all([
stopApp(context.server),
killApp(devContext.server),
stopApp(context.serverNoTrailSlash),
])
})
it('should honor exportTrailingSlash for 404 page', async () => {
expect(await fileExist(join(outdir, '404/index.html'))).toBe(true)
// we still output 404.html for backwards compat
expect(await fileExist(join(outdir, '404.html'))).toBe(true)
})
it('should handle trailing slash in getStaticPaths', async () => {
expect(await fileExist(join(outdir, 'gssp/foo/index.html'))).toBe(true)
expect(await fileExist(join(outNoTrailSlash, 'gssp/foo.html'))).toBe(true)
})
it('should only output 404.html without exportTrailingSlash', async () => {
expect(await fileExist(join(outNoTrailSlash, '404/index.html'))).toBe(false)
expect(await fileExist(join(outNoTrailSlash, '404.html'))).toBe(true)
})
it('should not duplicate /index with exportTrailingSlash', async () => {
expect(await fileExist(join(outdir, 'index/index.html'))).toBe(false)
expect(await fileExist(join(outdir, 'index.html'))).toBe(true)
})
ssr(context)
browser(context)
dev(devContext)
dynamic(context)
apiRoutes(context)
})