Skip to content

Commit 6795c91

Browse files
committed
chore: add onPreDev tests
1 parent 3058c24 commit 6795c91

File tree

4 files changed

+114
-11
lines changed

4 files changed

+114
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/src/helpers/dev.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'path'
1+
import { resolve } from 'path'
22

33
import type { OnPreBuild } from '@netlify/build'
44
import execa from 'execa'
@@ -15,7 +15,11 @@ export const onPreDev: OnPreBuild = async ({ constants, netlifyConfig }) => {
1515

1616
await writeDevEdgeFunction(constants)
1717
// Don't await this or it will never finish
18-
execa.node(join(__dirname, 'watcher.js'), [base], {
19-
stdio: 'inherit',
20-
})
18+
execa.node(
19+
resolve(__dirname, '..', '..', 'lib', 'helpers', 'watcher.js'),
20+
[base, process.env.NODE_ENV === 'test' ? '--once' : ''],
21+
{
22+
stdio: 'inherit',
23+
},
24+
)
2125
}

test/index.js renamed to test/index.spec.js

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ jest.mock('../packages/runtime/src/helpers/utils', () => {
99
})
1010

1111
const Chance = require('chance')
12-
const { writeJSON, unlink, existsSync, readFileSync, copy, ensureDir, readJson, pathExists } = require('fs-extra')
12+
const {
13+
writeJSON,
14+
unlink,
15+
existsSync,
16+
readFileSync,
17+
copy,
18+
ensureDir,
19+
readJson,
20+
pathExists,
21+
writeFile,
22+
} = require('fs-extra')
1323
const path = require('path')
1424
const process = require('process')
1525
const os = require('os')
@@ -35,7 +45,7 @@ const {
3545
updateRequiredServerFiles,
3646
generateCustomHeaders,
3747
} = require('../packages/runtime/src/helpers/config')
38-
const { dirname } = require('path')
48+
const { dirname, resolve } = require('path')
3949
const { getProblematicUserRewrites } = require('../packages/runtime/src/helpers/verification')
4050

4151
const chance = new Chance()
@@ -119,8 +129,12 @@ const rewriteAppDir = async function (dir = '.next') {
119129
}
120130

121131
// Move .next from sample project to current directory
122-
export const moveNextDist = async function (dir = '.next') {
123-
await stubModules(['next', 'sharp'])
132+
export const moveNextDist = async function (dir = '.next', copyMods = false) {
133+
if (copyMods) {
134+
await copyModules(['next', 'sharp'])
135+
} else {
136+
await stubModules(['next', 'sharp'])
137+
}
124138
await ensureDir(dirname(dir))
125139
await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), dir))
126140

@@ -134,6 +148,14 @@ export const moveNextDist = async function (dir = '.next') {
134148
await rewriteAppDir(dir)
135149
}
136150

151+
const copyModules = async function (modules) {
152+
for (const mod of modules) {
153+
const source = dirname(require.resolve(`${mod}/package.json`))
154+
const dest = path.join(process.cwd(), 'node_modules', mod)
155+
await copy(source, dest)
156+
}
157+
}
158+
137159
const stubModules = async function (modules) {
138160
for (const mod of modules) {
139161
const dir = path.join(process.cwd(), 'node_modules', mod)
@@ -182,7 +204,9 @@ afterEach(async () => {
182204
// Cleans up the temporary directory from `getTmpDir()` and do not make it
183205
// the current directory anymore
184206
restoreCwd()
185-
await cleanup()
207+
if (!process.env.TEST_SKIP_CLEANUP) {
208+
await cleanup()
209+
}
186210
})
187211

188212
describe('preBuild()', () => {
@@ -1719,3 +1743,78 @@ describe('api route file analysis', () => {
17191743
)
17201744
})
17211745
})
1746+
1747+
const middlewareSourceTs = /* typescript */ `
1748+
import { NextResponse } from 'next/server'
1749+
export async function middleware(req: NextRequest) {
1750+
return NextResponse.next()
1751+
}
1752+
`
1753+
1754+
const middlewareSourceJs = /* javascript */ `
1755+
import { NextResponse } from 'next/server'
1756+
export async function middleware(req) {
1757+
return NextResponse.next()
1758+
}
1759+
`
1760+
1761+
const wait = (seconds = 0.5) => new Promise((resolve) => setTimeout(resolve, seconds * 1000))
1762+
1763+
const middlewareExists = () => existsSync(resolve('.netlify', 'middleware.js'))
1764+
1765+
describe('onPreDev', () => {
1766+
let runtime
1767+
beforeAll(async () => {
1768+
runtime = await nextRuntimeFactory({}, { events: new Set(['onPreDev']) })
1769+
})
1770+
1771+
it('should generate the runtime with onPreDev', () => {
1772+
expect(runtime).toHaveProperty('onPreDev')
1773+
})
1774+
1775+
it('should run without middleware', async () => {
1776+
await moveNextDist('.next', true)
1777+
await runtime.onPreDev(defaultArgs)
1778+
// Allow time for esbuild to compile
1779+
await wait()
1780+
expect(middlewareExists()).toBeFalsy()
1781+
})
1782+
1783+
it('should compile middleware in the root directory', async () => {
1784+
await moveNextDist('.next', true)
1785+
await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs)
1786+
await runtime.onPreDev(defaultArgs)
1787+
await wait()
1788+
1789+
expect(middlewareExists()).toBeTruthy()
1790+
})
1791+
1792+
it('should compile middleware in the src directory', async () => {
1793+
await moveNextDist('.next', true)
1794+
await ensureDir(path.join(process.cwd(), 'src'))
1795+
await writeFile(path.join(process.cwd(), 'src', 'middleware.ts'), middlewareSourceTs)
1796+
await runtime.onPreDev(defaultArgs)
1797+
await wait()
1798+
1799+
expect(middlewareExists()).toBeTruthy()
1800+
})
1801+
1802+
it('should compile JS middleware in the root directory', async () => {
1803+
await moveNextDist('.next', true)
1804+
await writeFile(path.join(process.cwd(), 'middleware.js'), middlewareSourceJs)
1805+
await runtime.onPreDev(defaultArgs)
1806+
await wait()
1807+
1808+
expect(middlewareExists()).toBeTruthy()
1809+
})
1810+
1811+
it('should compile JS middleware in the src directory', async () => {
1812+
await moveNextDist('.next', true)
1813+
await ensureDir(path.join(process.cwd(), 'src'))
1814+
await writeFile(path.join(process.cwd(), 'src', 'middleware.js'), middlewareSourceJs)
1815+
await runtime.onPreDev(defaultArgs)
1816+
await wait()
1817+
1818+
expect(middlewareExists()).toBeTruthy()
1819+
})
1820+
})

0 commit comments

Comments
 (0)