Skip to content

Commit 663f8c7

Browse files
committed
chore: fix tests
1 parent e1d6c4e commit 663f8c7

File tree

3 files changed

+179
-47
lines changed

3 files changed

+179
-47
lines changed

src/helpers/files.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
const { cpus } = require('os')
33

44
const { yellowBright } = require('chalk')
5-
const { existsSync, readJson, move, copy, writeJson, readFile, writeFile, ensureDir } = require('fs-extra')
5+
const {
6+
existsSync,
7+
readJson,
8+
move,
9+
copy,
10+
writeJson,
11+
readFile,
12+
writeFile,
13+
ensureDir,
14+
readFileSync,
15+
} = require('fs-extra')
616
const globby = require('globby')
717
const { outdent } = require('outdent')
818
const pLimit = require('p-limit')
@@ -62,7 +72,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
6272
console.log('Moving static page files to serve from CDN...')
6373
const outputDir = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless')
6474
const root = join(outputDir, 'pages')
65-
const buildId = await readFile(join(netlifyConfig.build.publish, 'BUILD_ID'), 'utf8')
75+
const buildId = readFileSync(join(netlifyConfig.build.publish, 'BUILD_ID'), 'utf8').trim()
6676
const dataDir = join('_next', 'data', buildId)
6777
await ensureDir(dataDir)
6878
// Load the middleware manifest so we can check if a file matches it before moving

test/__snapshots__/index.js.snap

Lines changed: 160 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,46 +72,166 @@ exports.resolvePages = () => {
7272

7373
exports[`onBuild() generates static files manifest 1`] = `
7474
Array [
75-
"en/getStaticProps/1.html",
76-
"en/getStaticProps/1.json",
77-
"en/getStaticProps/2.html",
78-
"en/getStaticProps/2.json",
79-
"en/getStaticProps/env.html",
80-
"en/getStaticProps/env.json",
81-
"en/getStaticProps/static.html",
82-
"en/getStaticProps/static.json",
83-
"en/getStaticProps/withFallback/3.html",
84-
"en/getStaticProps/withFallback/3.json",
85-
"en/getStaticProps/withFallback/4.html",
86-
"en/getStaticProps/withFallback/4.json",
87-
"en/getStaticProps/withFallback/my/path/1.html",
88-
"en/getStaticProps/withFallback/my/path/1.json",
89-
"en/getStaticProps/withFallback/my/path/2.html",
90-
"en/getStaticProps/withFallback/my/path/2.json",
91-
"en/getStaticProps/withFallbackBlocking/3.html",
92-
"en/getStaticProps/withFallbackBlocking/3.json",
93-
"en/getStaticProps/withFallbackBlocking/4.html",
94-
"en/getStaticProps/withFallbackBlocking/4.json",
95-
"en/image.html",
96-
"en/previewTest.html",
97-
"en/previewTest.json",
98-
"en/static.html",
99-
"es/getStaticProps/env.html",
100-
"es/getStaticProps/env.json",
101-
"es/getStaticProps/static.html",
102-
"es/getStaticProps/static.json",
103-
"es/image.html",
104-
"es/previewTest.html",
105-
"es/previewTest.json",
106-
"es/static.html",
107-
"fr/getStaticProps/env.html",
108-
"fr/getStaticProps/env.json",
109-
"fr/getStaticProps/static.html",
110-
"fr/getStaticProps/static.json",
111-
"fr/image.html",
112-
"fr/previewTest.html",
113-
"fr/previewTest.json",
114-
"fr/static.html",
75+
Array [
76+
"en/getStaticProps/1.html",
77+
"en/getStaticProps/1.html",
78+
],
79+
Array [
80+
"en/getStaticProps/1.json",
81+
"_next/data/build-id/en/getStaticProps/1.json",
82+
],
83+
Array [
84+
"en/getStaticProps/2.html",
85+
"en/getStaticProps/2.html",
86+
],
87+
Array [
88+
"en/getStaticProps/2.json",
89+
"_next/data/build-id/en/getStaticProps/2.json",
90+
],
91+
Array [
92+
"en/getStaticProps/env.html",
93+
"en/getStaticProps/env.html",
94+
],
95+
Array [
96+
"en/getStaticProps/env.json",
97+
"_next/data/build-id/en/getStaticProps/env.json",
98+
],
99+
Array [
100+
"en/getStaticProps/static.html",
101+
"en/getStaticProps/static.html",
102+
],
103+
Array [
104+
"en/getStaticProps/static.json",
105+
"_next/data/build-id/en/getStaticProps/static.json",
106+
],
107+
Array [
108+
"en/getStaticProps/withFallback/3.html",
109+
"en/getStaticProps/withFallback/3.html",
110+
],
111+
Array [
112+
"en/getStaticProps/withFallback/3.json",
113+
"_next/data/build-id/en/getStaticProps/withFallback/3.json",
114+
],
115+
Array [
116+
"en/getStaticProps/withFallback/4.html",
117+
"en/getStaticProps/withFallback/4.html",
118+
],
119+
Array [
120+
"en/getStaticProps/withFallback/4.json",
121+
"_next/data/build-id/en/getStaticProps/withFallback/4.json",
122+
],
123+
Array [
124+
"en/getStaticProps/withFallback/my/path/1.html",
125+
"en/getStaticProps/withFallback/my/path/1.html",
126+
],
127+
Array [
128+
"en/getStaticProps/withFallback/my/path/1.json",
129+
"_next/data/build-id/en/getStaticProps/withFallback/my/path/1.json",
130+
],
131+
Array [
132+
"en/getStaticProps/withFallback/my/path/2.html",
133+
"en/getStaticProps/withFallback/my/path/2.html",
134+
],
135+
Array [
136+
"en/getStaticProps/withFallback/my/path/2.json",
137+
"_next/data/build-id/en/getStaticProps/withFallback/my/path/2.json",
138+
],
139+
Array [
140+
"en/getStaticProps/withFallbackBlocking/3.html",
141+
"en/getStaticProps/withFallbackBlocking/3.html",
142+
],
143+
Array [
144+
"en/getStaticProps/withFallbackBlocking/3.json",
145+
"_next/data/build-id/en/getStaticProps/withFallbackBlocking/3.json",
146+
],
147+
Array [
148+
"en/getStaticProps/withFallbackBlocking/4.html",
149+
"en/getStaticProps/withFallbackBlocking/4.html",
150+
],
151+
Array [
152+
"en/getStaticProps/withFallbackBlocking/4.json",
153+
"_next/data/build-id/en/getStaticProps/withFallbackBlocking/4.json",
154+
],
155+
Array [
156+
"en/image.html",
157+
"en/image.html",
158+
],
159+
Array [
160+
"en/previewTest.html",
161+
"en/previewTest.html",
162+
],
163+
Array [
164+
"en/previewTest.json",
165+
"_next/data/build-id/en/previewTest.json",
166+
],
167+
Array [
168+
"en/static.html",
169+
"en/static.html",
170+
],
171+
Array [
172+
"es/getStaticProps/env.html",
173+
"es/getStaticProps/env.html",
174+
],
175+
Array [
176+
"es/getStaticProps/env.json",
177+
"_next/data/build-id/es/getStaticProps/env.json",
178+
],
179+
Array [
180+
"es/getStaticProps/static.html",
181+
"es/getStaticProps/static.html",
182+
],
183+
Array [
184+
"es/getStaticProps/static.json",
185+
"_next/data/build-id/es/getStaticProps/static.json",
186+
],
187+
Array [
188+
"es/image.html",
189+
"es/image.html",
190+
],
191+
Array [
192+
"es/previewTest.html",
193+
"es/previewTest.html",
194+
],
195+
Array [
196+
"es/previewTest.json",
197+
"_next/data/build-id/es/previewTest.json",
198+
],
199+
Array [
200+
"es/static.html",
201+
"es/static.html",
202+
],
203+
Array [
204+
"fr/getStaticProps/env.html",
205+
"fr/getStaticProps/env.html",
206+
],
207+
Array [
208+
"fr/getStaticProps/env.json",
209+
"_next/data/build-id/fr/getStaticProps/env.json",
210+
],
211+
Array [
212+
"fr/getStaticProps/static.html",
213+
"fr/getStaticProps/static.html",
214+
],
215+
Array [
216+
"fr/getStaticProps/static.json",
217+
"_next/data/build-id/fr/getStaticProps/static.json",
218+
],
219+
Array [
220+
"fr/image.html",
221+
"fr/image.html",
222+
],
223+
Array [
224+
"fr/previewTest.html",
225+
"fr/previewTest.html",
226+
],
227+
Array [
228+
"fr/previewTest.json",
229+
"_next/data/build-id/fr/previewTest.json",
230+
],
231+
Array [
232+
"fr/static.html",
233+
"fr/static.html",
234+
],
115235
]
116236
`;
117237

test/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,11 @@ describe('onBuild()', () => {
206206
test("fails if BUILD_ID doesn't exist", async () => {
207207
await moveNextDist()
208208
await unlink(path.join(process.cwd(), '.next/BUILD_ID'))
209-
const failBuild = jest.fn()
210-
await plugin.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })
209+
const failBuild = jest.fn().mockImplementation(() => {
210+
throw new Error('BUILD_ID does not exist')
211+
})
212+
213+
expect(() => plugin.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow()
211214
expect(failBuild).toHaveBeenCalled()
212215
})
213216

@@ -260,8 +263,7 @@ describe('onBuild()', () => {
260263
await moveNextDist()
261264
await plugin.onBuild(defaultArgs)
262265
const data = JSON.parse(readFileSync(path.resolve('.next/static-manifest.json'), 'utf8'))
263-
264-
data.forEach((file) => {
266+
data.forEach(([_, file]) => {
265267
expect(existsSync(path.resolve(path.join('.next', file)))).toBeTruthy()
266268
expect(existsSync(path.resolve(path.join('.next', 'server', 'pages', file)))).toBeFalsy()
267269
})
@@ -274,7 +276,7 @@ describe('onBuild()', () => {
274276

275277
const locale = 'en/'
276278

277-
data.forEach((file) => {
279+
data.forEach(([_, file]) => {
278280
if (!file.startsWith(locale)) {
279281
return
280282
}

0 commit comments

Comments
 (0)