Skip to content

Commit b3a0d3e

Browse files
committed
test
1 parent e810b4b commit b3a0d3e

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

helpers/getNextConfig.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const { resolve } = require('path')
44

55
const moize = require('moize')
66

7-
// Load next.config.js
8-
const getNextConfig = async function (failBuild = defaultFailBuild) {
7+
// cwd argument needed for moize with tests
8+
const getNextConfig = async function (cwd = process.cwd(), failBuild = defaultFailBuild) {
99
// We cannot load `next` at the top-level because we validate whether the
1010
// site is using `next` inside `onPreBuild`.
1111
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
@@ -24,4 +24,5 @@ const defaultFailBuild = function (message, { error }) {
2424
throw new Error(`${message}\n${error.stack}`)
2525
}
2626

27+
// module.exports = process.env.NODE_ENV === 'test' ? getNextConfig : moizedGetNextConfig
2728
module.exports = moizedGetNextConfig

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
return failBuild('Could not find a package.json for this project')
2929
}
3030

31-
const nextConfig = await getNextConfig(utils.failBuild)
31+
const nextConfig = await getNextConfig(process.cwd(), utils.failBuild)
3232
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir })
3333

3434
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
@@ -70,7 +70,7 @@ module.exports = {
7070
return
7171
}
7272

73-
const nextConfig = await getNextConfig(utils.failBuild)
73+
const nextConfig = await getNextConfig(process.cwd(), utils.failBuild)
7474
await saveCache({cache: utils.cache, distDir: nextConfig.distDir })
7575
copyUnstableIncludedDirs({ nextConfig, functionsDist: FUNCTIONS_DIST })
7676
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
target: 'serverless',
3+
distDir: 'build',
4+
}

test/index.js

+47
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ describe('preBuild()', () => {
159159
}),
160160
).rejects.toThrow(`Error loading your next.config.js.`)
161161
})
162+
163+
test('restores cache with right paths', async () => {
164+
await useFixture('dist_dir_next_config')
165+
166+
let distPath
167+
const utils_ = { ...utils, cache: {
168+
restore: (x) => distPath = x } }
169+
const spy = jest.spyOn(utils_.cache, 'restore')
170+
171+
172+
await plugin.onPreBuild({
173+
netlifyConfig,
174+
packageJson: DUMMY_PACKAGE_JSON,
175+
utils: utils_,
176+
constants: { FUNCTIONS_SRC: 'out_functions' },
177+
})
178+
179+
expect(spy).toHaveBeenCalled()
180+
expect(distPath).toBe('build/cache')
181+
})
162182
})
163183

164184
describe('onBuild()', () => {
@@ -233,3 +253,30 @@ describe('onBuild()', () => {
233253
expect(await pathExists(`${resolvedFunctions}/next_api_test/next_api_test.js`)).toBeTruthy()
234254
})
235255
})
256+
257+
describe('onPostBuild', () => {
258+
test('saves cache with right paths', async () => {
259+
await useFixture('dist_dir_next_config')
260+
261+
let distPath
262+
let manifestPath
263+
const utils_ = { ...utils, cache: {
264+
save: (x, y) => {
265+
distPath = x
266+
manifestPath = y
267+
} } }
268+
const spy = jest.spyOn(utils_.cache, 'save')
269+
270+
271+
await plugin.onPostBuild({
272+
netlifyConfig,
273+
packageJson: DUMMY_PACKAGE_JSON,
274+
utils: utils_,
275+
constants: { FUNCTIONS_SRC: 'out_functions' },
276+
})
277+
278+
expect(spy).toHaveBeenCalled()
279+
expect(distPath).toBe('build/cache')
280+
expect(manifestPath.digests[0]).toBe('build/build-manifest.json')
281+
})
282+
})

0 commit comments

Comments
 (0)