Commit fc4618f8 authored by Tobias Koppers's avatar Tobias Koppers

Add integration test for multi-project dev server

Adds an E2E test that spawns `next dev` with two `--experimental-project`
flags, waits for both workers to become ready, then verifies each project
serves its routes correctly on its assigned port:
- Project 1 serves "Project 1" and not "Project 2"
- Project 2 serves "Project 2" and not "Project 1"
- Both workers printed the "Ready in" startup line

The test is gated behind `hasDaemonSupport()` which checks if the native
bindings export `startTurbopackDaemon` — this is only available when
bindings are compiled from this branch (pre-built npm binaries skip it).
On CI with compiled bindings the tests run; locally they're skipped.

Also adds `.allowUnknownOption()` to both the `dev` and `build` Commander
commands so `--experimental-project` flags pass through to the manual
argv parser in `getProjectGroups()`.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent 263c061f
......@@ -234,6 +234,9 @@ program
'Internal: connect to turbopack daemon'
).hideHelp()
)
// --experimental-project flags are parsed manually by getProjectGroups()
// before Commander sees them, so we must allow unknown options here.
.allowUnknownOption()
.action((directory: string, options: NextBuildOptions) => {
if (options.debugPrerender) {
// @ts-expect-error not readonly
......@@ -404,6 +407,9 @@ program
'Internal: connect to turbopack daemon'
).hideHelp()
)
// --experimental-project flags are parsed manually by getProjectGroups()
// before Commander sees them, so we must allow unknown options here.
.allowUnknownOption()
.action(
(directory: string, options: NextDevOptions, { _optionValueSources }) => {
if (options.experimentalNextConfigStripTypes) {
......
import { spawn, ChildProcess } from 'child_process'
import path from 'path'
import { findPort, fetchViaHTTP, killApp } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
import { parseProjectGroups } from '../../../packages/next/src/lib/multi-project'
const fixturesDir = path.join(__dirname, 'fixtures')
const nextDir = path.dirname(require.resolve('next/package'))
const nextBin = path.join(nextDir, 'dist/bin/next')
/** Check whether the native bindings support the daemon (they must be locally
* compiled from this branch — pre-built npm binaries won't have the new API). */
function hasDaemonSupport(): boolean {
try {
const bindings = require('next/dist/build/swc')
return typeof bindings.turbo?.startTurbopackDaemon === 'function'
} catch {
return false
}
}
const describeDaemon = hasDaemonSupport() ? describe : describe.skip
describeDaemon('multi-project dev server', () => {
let devProcess: ChildProcess
let port1: number
let port2: number
let output = ''
beforeAll(async () => {
port1 = await findPort()
port2 = await findPort()
devProcess = spawn(
process.execPath,
[
'--no-deprecation',
nextBin,
'dev',
'--experimental-project',
path.join(fixturesDir, 'proj1'),
'--port',
String(port1),
'--experimental-project',
path.join(fixturesDir, 'proj2'),
'--port',
String(port2),
],
{
stdio: ['ignore', 'pipe', 'pipe'],
env: {
...process.env,
NODE_ENV: undefined,
__NEXT_TEST_MODE: 'true',
NEXT_TELEMETRY_DISABLED: '1',
},
}
)
// Wait for both workers to print "Ready in"
await new Promise<void>((resolve, reject) => {
let readyCount = 0
let settled = false
const settle = (fn: () => void) => {
if (!settled) {
settled = true
clearTimeout(timer)
fn()
}
}
devProcess.stdout!.on('data', (chunk: Buffer) => {
const msg = chunk.toString()
output += msg
process.stdout.write(chunk)
const stripped = stripAnsi(msg)
const matches = stripped.match(/✓ Ready in/gi) ?? []
readyCount += matches.length
if (readyCount >= 2) {
settle(resolve)
}
})
devProcess.stderr!.on('data', (chunk: Buffer) => {
output += chunk.toString()
process.stderr.write(chunk)
})
devProcess.on('error', (err) => settle(() => reject(err)))
devProcess.on('close', (code) => {
settle(() =>
reject(
new Error(
`next dev exited with code ${code} before both projects were ready.\nOutput:\n${output}`
)
)
)
})
const timer = setTimeout(() => {
settle(() =>
reject(
new Error(
`Timed out waiting for both projects to be ready (readyCount=${readyCount}).\nOutput:\n${output}`
)
)
)
}, 120_000)
timer.unref()
})
}, 180_000)
afterAll(async () => {
await killApp(devProcess)
})
it('serves project 1 on its port', async () => {
const res = await fetchViaHTTP(port1, '/')
expect(res.status).toBe(200)
const html = await res.text()
expect(html).toContain('Project 1')
expect(html).not.toContain('Project 2')
})
it('serves project 2 on its port', async () => {
const res = await fetchViaHTTP(port2, '/')
expect(res.status).toBe(200)
const html = await res.text()
expect(html).toContain('Project 2')
expect(html).not.toContain('Project 1')
})
it('both workers started', () => {
const stripped = stripAnsi(output)
const matches = stripped.match(/✓ Ready in/gi) ?? []
expect(matches.length).toBeGreaterThanOrEqual(2)
})
})
describe('parseProjectGroups', () => {
/** Shorthand that prepends the standard ['node', 'next', 'dev'] prefix. */
const parse = (...args: string[]) =>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment