Skip to content

Commit 9e67e2e

Browse files
committed
chore: create describeCwdTmpDir helper
1 parent 28309de commit 9e67e2e

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

test/helpers/files.spec.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { dirname } from "path"
1717
import { resolve } from 'pathe'
1818
import { join } from "pathe"
1919
import { Rewrites } from "../../packages/runtime/src/helpers/types"
20+
import { describeCwdTmpDir, moveNextDist } from "../test-utils"
2021

2122
const REDIRECTS: Rewrites = [
2223
{
@@ -187,21 +188,22 @@ describe('files utility functions', () => {
187188
expect(matchesRewrite(path, REWRITES)).toBeTruthy()
188189
})
189190
})
191+
})
190192

191-
test.only('patches Next server files', async () => {
192-
const root = path.resolve(dirname(__dirname))
193-
console.log({ root })
194-
// await copy(join(root, 'package.json'), path.join(process.cwd(), 'package.json'))
195-
// await ensureDir(path.join(process.cwd(), 'node_modules'))
196-
// await copy(path.join(root, 'node_modules', 'next'), path.join(process.cwd(), 'node_modules', 'next'))
193+
describeCwdTmpDir('file patching', () => {
194+
it('patches Next server files', async () => {
195+
const root = path.resolve(dirname(resolve(__dirname, '..')))
196+
await copy(join(root, 'package.json'), path.join(process.cwd(), 'package.json'))
197+
await ensureDir(path.join(process.cwd(), 'node_modules'))
198+
await copy(path.join(root, 'node_modules', 'next'), path.join(process.cwd(), 'node_modules', 'next'))
197199

198-
// await patchNextFiles(process.cwd())
200+
await patchNextFiles(process.cwd())
199201
const serverFile = path.resolve(process.cwd(), 'node_modules', 'next', 'dist', 'server', 'base-server.js')
200202
const patchedData = await readFileSync(serverFile, 'utf8')
201203
expect(patchedData.includes('_REVALIDATE_SSG')).toBeTruthy()
202204
expect(patchedData.includes('private: isPreviewMode && cachedData')).toBeTruthy()
203205

204-
// await unpatchNextFiles(process.cwd())
206+
await unpatchNextFiles(process.cwd())
205207

206208
const unPatchedData = await readFileSync(serverFile, 'utf8')
207209
expect(unPatchedData.includes('_REVALIDATE_SSG')).toBeFalsy()
@@ -213,7 +215,7 @@ describe('dependency tracing', () => {
213215
it('generates dependency list from a source file', async () => {
214216
const dependencies = await getDependenciesOfFile(resolve(__dirname, '../fixtures/analysis/background.js'))
215217
expect(dependencies).toEqual(
216-
['test/webpack-api-runtime.js', 'package.json'].map((dep) => resolve(dirname(__dirname), dep)),
218+
['test/webpack-api-runtime.js', 'package.json'].map((dep) => resolve(dirname(resolve(__dirname, '..')), dep)),
217219
)
218220
})
219221
})

test/test-utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
readJson,
99
copy,
1010
} from "fs-extra"
11+
import { dir as getTmpDir } from "tmp-promise"
1112

1213
const FIXTURES_DIR = `${__dirname}/fixtures`
1314
const SAMPLE_PROJECT_DIR = `${__dirname}/../demos/default`
@@ -69,4 +70,25 @@ export const stubModules = async function (modules) {
6970
export const useFixture = async function (fixtureName) {
7071
const fixtureDir = `${FIXTURES_DIR}/${fixtureName}`
7172
await cpy('**', process.cwd(), { cwd: fixtureDir, parents: true, overwrite: true, dot: true })
73+
}
74+
75+
// Change current cwd() to a temporary directory
76+
export const describeCwdTmpDir = (name: string, fn: () => void): void => {
77+
describe(name, () => {
78+
let restoreCwd
79+
let cleanup
80+
81+
beforeEach(async () => {
82+
const tmpDir = await getTmpDir({ unsafeCleanup: true })
83+
restoreCwd = changeCwd(tmpDir.path)
84+
cleanup = tmpDir.cleanup
85+
})
86+
87+
afterEach(async () => {
88+
restoreCwd()
89+
await cleanup()
90+
})
91+
92+
fn()
93+
})
7294
}

0 commit comments

Comments
 (0)