Skip to content

Commit ec4bc1f

Browse files
committed
chore: move test-utils to own file and move functions to own file
1 parent 63b5d69 commit ec4bc1f

File tree

3 files changed

+98
-87
lines changed

3 files changed

+98
-87
lines changed

test/helpers/functions.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { getExtendedApiRouteConfigs } from "../../packages/runtime/src/helpers/functions"
2+
import { moveNextDist } from "../test-utils"
3+
4+
describe('api route file analysis', () => {
5+
it('extracts correct route configs from source files', async () => {
6+
await moveNextDist()
7+
const configs = await getExtendedApiRouteConfigs('.next', process.cwd())
8+
// Using a Set means the order doesn't matter
9+
expect(new Set(configs)).toEqual(
10+
new Set([
11+
{
12+
compiled: 'pages/api/hello-background.js',
13+
config: { type: 'experimental-background' },
14+
route: '/api/hello-background',
15+
},
16+
{
17+
compiled: 'pages/api/hello-scheduled.js',
18+
config: { schedule: '@hourly', type: 'experimental-scheduled' },
19+
route: '/api/hello-scheduled',
20+
},
21+
]),
22+
)
23+
})
24+
})

test/index.spec.ts

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
unlink,
1515
existsSync,
1616
readFileSync,
17-
copy,
1817
ensureDir,
1918
readJson,
2019
pathExists,
@@ -24,9 +23,7 @@ import {
2423
import path from "path"
2524
import process from "process"
2625
import os from "os"
27-
import cpy from "cpy"
2826
import { dir as getTmpDir } from "tmp-promise"
29-
import { getExtendedApiRouteConfigs } from "../packages/runtime/src/helpers/functions"
3027
// @ts-expect-error - TODO: Convert runtime export to ES6
3128
import nextRuntimeFactory from "../packages/runtime/src"
3229
const nextRuntime = nextRuntimeFactory({})
@@ -38,13 +35,12 @@ import {
3835
updateRequiredServerFiles,
3936
generateCustomHeaders,
4037
} from "../packages/runtime/src/helpers/config"
41-
import { dirname, resolve } from "path"
38+
import { resolve } from "path"
4239
import { getProblematicUserRewrites } from "../packages/runtime/src/helpers/verification"
4340
import type { NetlifyPluginOptions } from '@netlify/build'
41+
import { changeCwd, useFixture, moveNextDist } from "./test-utils"
4442

4543
const chance = new Chance()
46-
const FIXTURES_DIR = `${__dirname}/fixtures`
47-
const SAMPLE_PROJECT_DIR = `${__dirname}/../demos/default`
4844
const constants = {
4945
INTERNAL_FUNCTIONS_SRC: '.netlify/functions-internal',
5046
PUBLISH_DIR: '.next',
@@ -65,68 +61,9 @@ const utils = {
6561

6662
const normalizeChunkNames = (source) => source.replaceAll(/\/chunks\/\d+\.js/g, '/chunks/CHUNK_ID.js')
6763

68-
// Temporary switch cwd
69-
const changeCwd = function (cwd) {
70-
const originalCwd = process.cwd()
71-
process.chdir(cwd)
72-
return () => {
73-
process.chdir(originalCwd)
74-
}
75-
}
76-
7764
const onBuildHasRun = (netlifyConfig) =>
7865
Boolean(netlifyConfig.functions[HANDLER_FUNCTION_NAME]?.included_files?.some((file) => file.includes('BUILD_ID')))
7966

80-
const rewriteAppDir = async function (dir = '.next') {
81-
const manifest = path.join(dir, 'required-server-files.json')
82-
const manifestContent = await readJson(manifest)
83-
manifestContent.appDir = process.cwd()
84-
85-
await writeJSON(manifest, manifestContent)
86-
}
87-
88-
// Move .next from sample project to current directory
89-
export const moveNextDist = async function (dir = '.next', copyMods = false) {
90-
if (copyMods) {
91-
await copyModules(['next', 'sharp'])
92-
} else {
93-
await stubModules(['next', 'sharp'])
94-
}
95-
await ensureDir(dirname(dir))
96-
await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), dir))
97-
98-
for (const file of ['pages', 'app', 'src', 'components', 'public', 'components', 'hello.txt', 'package.json']) {
99-
const source = path.join(SAMPLE_PROJECT_DIR, file)
100-
if (existsSync(source)) {
101-
await copy(source, path.join(process.cwd(), file))
102-
}
103-
}
104-
105-
await rewriteAppDir(dir)
106-
}
107-
108-
const copyModules = async function (modules) {
109-
for (const mod of modules) {
110-
const source = dirname(require.resolve(`${mod}/package.json`))
111-
const dest = path.join(process.cwd(), 'node_modules', mod)
112-
await copy(source, dest)
113-
}
114-
}
115-
116-
const stubModules = async function (modules) {
117-
for (const mod of modules) {
118-
const dir = path.join(process.cwd(), 'node_modules', mod)
119-
await ensureDir(dir)
120-
await writeJSON(path.join(dir, 'package.json'), { name: mod })
121-
}
122-
}
123-
124-
// Copy fixture files to the current directory
125-
const useFixture = async function (fixtureName) {
126-
const fixtureDir = `${FIXTURES_DIR}/${fixtureName}`
127-
await cpy('**', process.cwd(), { cwd: fixtureDir, parents: true, overwrite: true, dot: true })
128-
}
129-
13067
const netlifyConfig = { build: { command: 'npm run build' }, functions: {}, redirects: [], headers: [] } as NetlifyPluginOptions["netlifyConfig"]
13168
const defaultArgs = {
13269
netlifyConfig,
@@ -1528,28 +1465,6 @@ describe('function helpers', () => {
15281465
})
15291466
})
15301467

1531-
describe('api route file analysis', () => {
1532-
it('extracts correct route configs from source files', async () => {
1533-
await moveNextDist()
1534-
const configs = await getExtendedApiRouteConfigs('.next', process.cwd())
1535-
// Using a Set means the order doesn't matter
1536-
expect(new Set(configs)).toEqual(
1537-
new Set([
1538-
{
1539-
compiled: 'pages/api/hello-background.js',
1540-
config: { type: 'experimental-background' },
1541-
route: '/api/hello-background',
1542-
},
1543-
{
1544-
compiled: 'pages/api/hello-scheduled.js',
1545-
config: { schedule: '@hourly', type: 'experimental-scheduled' },
1546-
route: '/api/hello-scheduled',
1547-
},
1548-
]),
1549-
)
1550-
})
1551-
})
1552-
15531468
const middlewareSourceTs = /* typescript */ `
15541469
import { NextResponse } from 'next/server'
15551470
export async function middleware(req: NextRequest) {

test/test-utils.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import path from "path"
2+
import { dirname } from "path"
3+
import cpy from "cpy"
4+
import {
5+
writeJSON,
6+
existsSync,
7+
ensureDir,
8+
readJson,
9+
copy,
10+
} from "fs-extra"
11+
12+
const FIXTURES_DIR = `${__dirname}/fixtures`
13+
const SAMPLE_PROJECT_DIR = `${__dirname}/../demos/default`
14+
15+
// Temporary switch cwd
16+
export const changeCwd = function (cwd) {
17+
const originalCwd = process.cwd()
18+
process.chdir(cwd)
19+
return () => {
20+
process.chdir(originalCwd)
21+
}
22+
}
23+
24+
const rewriteAppDir = async function (dir = '.next') {
25+
const manifest = path.join(dir, 'required-server-files.json')
26+
const manifestContent = await readJson(manifest)
27+
manifestContent.appDir = process.cwd()
28+
29+
await writeJSON(manifest, manifestContent)
30+
}
31+
32+
// Move .next from sample project to current directory
33+
export const moveNextDist = async function (dir = '.next', copyMods = false) {
34+
if (copyMods) {
35+
await copyModules(['next', 'sharp'])
36+
} else {
37+
await stubModules(['next', 'sharp'])
38+
}
39+
await ensureDir(dirname(dir))
40+
await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), dir))
41+
42+
for (const file of ['pages', 'app', 'src', 'components', 'public', 'components', 'hello.txt', 'package.json']) {
43+
const source = path.join(SAMPLE_PROJECT_DIR, file)
44+
if (existsSync(source)) {
45+
await copy(source, path.join(process.cwd(), file))
46+
}
47+
}
48+
49+
await rewriteAppDir(dir)
50+
}
51+
52+
export const copyModules = async function (modules) {
53+
for (const mod of modules) {
54+
const source = dirname(require.resolve(`${mod}/package.json`))
55+
const dest = path.join(process.cwd(), 'node_modules', mod)
56+
await copy(source, dest)
57+
}
58+
}
59+
60+
export const stubModules = async function (modules) {
61+
for (const mod of modules) {
62+
const dir = path.join(process.cwd(), 'node_modules', mod)
63+
await ensureDir(dir)
64+
await writeJSON(path.join(dir, 'package.json'), { name: mod })
65+
}
66+
}
67+
68+
// Copy fixture files to the current directory
69+
export const useFixture = async function (fixtureName) {
70+
const fixtureDir = `${FIXTURES_DIR}/${fixtureName}`
71+
await cpy('**', process.cwd(), { cwd: fixtureDir, parents: true, overwrite: true, dot: true })
72+
}

0 commit comments

Comments
 (0)