-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathdefaults.test.js
217 lines (181 loc) · 8.73 KB
/
defaults.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Test default next-on-netlify configuration
const { parse, join } = require('path')
const { existsSync, readdirSync, readFileSync, readJsonSync } = require('fs-extra')
const buildNextApp = require('./helpers/buildNextApp')
// The name of this test file (without extension)
const FILENAME = parse(__filename).name
// The directory which will be used for testing.
// We simulate a NextJS app within that directory, with pages, and a
// package.json file.
const PROJECT_PATH = join(__dirname, 'builds', FILENAME)
// Capture the output to verify successful build
let buildOutput
beforeAll(
async () => {
buildOutput = await buildNextApp()
.forTest(__filename)
.withPages('pages')
.withNextConfig('next.config.js')
.withPackageJson('package.json')
.withFile('image.png', join('public', 'image.png'))
.build()
},
// time out after 180 seconds
180 * 1000,
)
describe('next-on-netlify', () => {
test('builds successfully', () => {
expect(buildOutput).toMatch('Next on Netlify')
expect(buildOutput).toMatch('Success! All done!')
})
})
describe('next-on-netlify', () => {
test('builds successfully', () => {
expect(buildOutput).toMatch('Next on Netlify')
expect(buildOutput).toMatch(`Copying public folder to out_publish`)
expect(buildOutput).toMatch(`Copying static NextJS assets to out_publish`)
expect(buildOutput).toMatch(`Setting up API endpoints as Netlify Functions in out_functions`)
expect(buildOutput).toMatch(`Setting up pages with getInitialProps as Netlify Functions in out_functions`)
expect(buildOutput).toMatch(`Setting up pages with getServerSideProps as Netlify Functions in out_functions`)
expect(buildOutput).toMatch(`Copying pre-rendered pages with getStaticProps and JSON data to out_publish`)
expect(buildOutput).toMatch(
`Setting up pages with getStaticProps and fallback: true as Netlify Functions in out_functions`,
)
expect(buildOutput).toMatch(
`Setting up pages with getStaticProps and revalidation interval as Netlify Functions in out_functions`,
)
expect(buildOutput).toMatch(`Copying pre-rendered pages without props to out_publish`)
expect(buildOutput).toMatch('Setting up redirects')
expect(buildOutput).toMatch('Success! All done!')
})
})
describe('SSR Pages', () => {
const functionsDir = join(PROJECT_PATH, 'out_functions')
test('creates a Netlify Function for each SSR page', () => {
expect(existsSync(join(functionsDir, 'next_index', 'next_index.js'))).toBe(true)
expect(existsSync(join(functionsDir, 'next_shows_id', 'next_shows_id.js'))).toBe(true)
expect(existsSync(join(functionsDir, 'next_shows_params', 'next_shows_params.js'))).toBe(true)
expect(existsSync(join(functionsDir, 'next_getServerSideProps_static', 'next_getServerSideProps_static.js'))).toBe(
true,
)
expect(existsSync(join(functionsDir, 'next_getServerSideProps_id', 'next_getServerSideProps_id.js'))).toBe(true)
expect(
readFileSync(join(functionsDir, 'next_getServerSideProps_id', 'nextPage', 'index.js'), 'utf-8'),
).toBe(`module.exports = require("./pages/getServerSideProps/[id].js")`)
})
})
describe('API Pages', () => {
const functionsDir = join(PROJECT_PATH, 'out_functions')
test('creates a Netlify Function for each API endpoint', () => {
expect(existsSync(join(functionsDir, 'next_api_static', 'next_api_static.js'))).toBe(true)
expect(existsSync(join(functionsDir, 'next_api_shows_id', 'next_api_shows_id.js'))).toBe(true)
expect(existsSync(join(functionsDir, 'next_api_shows_params', 'next_api_shows_params.js'))).toBe(true)
expect(existsSync(join(functionsDir, 'next_api_hello-background', 'next_api_hello-background.js'))).toBe(true)
})
})
describe('next/image', () => {
const functionsDir = join(PROJECT_PATH, 'out_functions')
test('sets up next_image as a function in every project by default', () => {
expect(existsSync(join(functionsDir, 'next_image', 'next_image.js'))).toBe(true)
})
test('injects the image plugin config into the function', () => {
expect(readJsonSync(join(functionsDir, 'next_image', 'imageconfig.json')).domains).toEqual(["placekitten.com"])
})
})
describe('SSG Pages with getStaticProps', () => {
test('creates pre-rendered HTML file in output directory', () => {
const OUTPUT_PATH = join(PROJECT_PATH, 'out_publish')
expect(existsSync(join(OUTPUT_PATH, 'getStaticProps', 'static.html'))).toBe(true)
expect(existsSync(join(OUTPUT_PATH, 'getStaticProps', '1.html'))).toBe(true)
expect(existsSync(join(OUTPUT_PATH, 'getStaticProps', '2.html'))).toBe(true)
expect(existsSync(join(OUTPUT_PATH, 'getStaticProps', 'withFallback', '3.html'))).toBe(true)
expect(existsSync(join(OUTPUT_PATH, 'getStaticProps', 'withFallback', '4.html'))).toBe(true)
expect(existsSync(join(OUTPUT_PATH, 'getStaticProps', 'withFallback', 'my', 'path', '1.html'))).toBe(true)
expect(existsSync(join(OUTPUT_PATH, 'getStaticProps', 'withFallback', 'my', 'path', '2.html'))).toBe(true)
})
test('creates data .json file in /_next/data/ directory', () => {
// Get path to data files
const dirs = readdirSync(join(PROJECT_PATH, 'out_publish', '_next', 'data'))
expect(dirs.length).toBe(1)
const dataDir = join(PROJECT_PATH, 'out_publish', '_next', 'data', dirs[0])
expect(existsSync(join(dataDir, 'getStaticProps', 'static.json'))).toBe(true)
expect(existsSync(join(dataDir, 'getStaticProps', '1.json'))).toBe(true)
expect(existsSync(join(dataDir, 'getStaticProps', '2.json'))).toBe(true)
expect(existsSync(join(dataDir, 'getStaticProps', 'withFallback', '3.json'))).toBe(true)
expect(existsSync(join(dataDir, 'getStaticProps', 'withFallback', '4.json'))).toBe(true)
expect(existsSync(join(dataDir, 'getStaticProps', 'withFallback', 'my', 'path', '1.json'))).toBe(true)
expect(existsSync(join(dataDir, 'getStaticProps', 'withFallback', 'my', 'path', '2.json'))).toBe(true)
})
test('creates Netlify Functions for pages with fallback', () => {
const functionPath1 = 'next_getStaticProps_withFallback_id/next_getStaticProps_withFallback_id.js'
expect(existsSync(join(PROJECT_PATH, 'out_functions', functionPath1))).toBe(true)
const functionPath2 = 'next_getStaticProps_withFallback_slug/next_getStaticProps_withFallback_slug.js'
expect(existsSync(join(PROJECT_PATH, 'out_functions', functionPath2))).toBe(true)
})
})
describe('SSG Pages with getStaticProps and revalidate', () => {
const functionsDir = join(PROJECT_PATH, 'out_functions')
test('creates a Netlify Function for each page', () => {
expect(
existsSync(join(functionsDir, 'next_getStaticProps_withrevalidate', 'next_getStaticProps_withrevalidate.js')),
).toBe(true)
expect(
existsSync(
join(functionsDir, 'next_getStaticProps_withRevalidate_id', 'next_getStaticProps_withRevalidate_id.js'),
),
).toBe(true)
expect(
existsSync(
join(
functionsDir,
'next_getStaticProps_withRevalidate_withFallback_id',
'next_getStaticProps_withRevalidate_withFallback_id.js',
),
),
).toBe(true)
})
})
describe('Static Pages', () => {
test('copies static pages to output directory', () => {
const OUTPUT_PATH = join(PROJECT_PATH, 'out_publish')
expect(existsSync(join(OUTPUT_PATH, 'static.html'))).toBe(true)
expect(existsSync(join(OUTPUT_PATH, 'static/[id].html'))).toBe(true)
})
test('copies static assets to out_publish/_next/ directory', () => {
const dirs = readdirSync(join(PROJECT_PATH, 'out_publish', '_next', 'static'))
expect(dirs.length).toBe(2)
expect(dirs).toContain('chunks')
})
})
describe('404 Page', () => {
test('copies 404.html to output directory', () => {
const OUTPUT_PATH = join(PROJECT_PATH, 'out_publish')
expect(existsSync(join(OUTPUT_PATH, '404.html'))).toBe(true)
})
})
describe('Public assets', () => {
test('copies public files to output directory', () => {
const OUTPUT_PATH = join(PROJECT_PATH, 'out_publish')
expect(existsSync(join(OUTPUT_PATH, 'image.png'))).toBe(true)
})
})
describe('Routing', () => {
test('creates Netlify redirects', async () => {
// Read _redirects file
const contents = readFileSync(join(PROJECT_PATH, 'out_publish', '_redirects'))
let redirects = contents.toString()
// Replace non-persistent build ID with placeholder
redirects = redirects.replace(/\/_next\/data\/[^\/]+\//g, '/_next/data/%BUILD_ID%/')
// Check that redirects match
expect(redirects).toMatchSnapshot()
})
})
describe('Headers', () => {
test('creates Netlify headers', async () => {
// Read _headers file
const contents = readFileSync(join(PROJECT_PATH, 'out_publish', '_headers'))
let headers = contents.toString()
// Check that headers match
expect(headers).toMatchSnapshot()
})
})