Unverified Commit f37f11ef authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Tests: Mirror arguments and environment between `next.build()` and `next.start()` (#83393)

## What?

Preparation for swapping the special logic in `start()` to using
`build()` directly.

The build() method now respects buildCommand and `NEXT_SKIP_ISOLATE`
settings that it previously ignored.
parent b03deadc
...@@ -8,7 +8,6 @@ import stripAnsi from 'strip-ansi' ...@@ -8,7 +8,6 @@ import stripAnsi from 'strip-ansi'
export class NextStartInstance extends NextInstance { export class NextStartInstance extends NextInstance {
private _buildId: string private _buildId: string
private _cliOutput: string = '' private _cliOutput: string = ''
private spawnOpts: import('child_process').SpawnOptions
public get buildId() { public get buildId() {
return this._buildId return this._buildId
...@@ -44,36 +43,10 @@ export class NextStartInstance extends NextInstance { ...@@ -44,36 +43,10 @@ export class NextStartInstance extends NextInstance {
} }
this._cliOutput = '' this._cliOutput = ''
this.spawnOpts = { const spawnOpts = this.getSpawnOpts()
cwd: this.testDir,
stdio: ['ignore', 'pipe', 'pipe'],
shell: false,
env: {
...process.env,
...this.env,
NODE_ENV: this.env.NODE_ENV || ('' as any),
...(this.forcedPort
? {
PORT: this.forcedPort,
}
: {
PORT: '0',
}),
__NEXT_TEST_MODE: 'e2e',
},
}
let buildArgs = ['pnpm', 'next', 'build']
let startArgs = ['pnpm', 'next', 'start'] let startArgs = ['pnpm', 'next', 'start']
if (this.buildCommand) {
buildArgs = this.buildCommand.split(' ')
}
if (this.buildArgs) {
buildArgs.push(...this.buildArgs)
}
if (this.startCommand) { if (this.startCommand) {
startArgs = this.startCommand.split(' ') startArgs = this.startCommand.split(' ')
} }
...@@ -84,23 +57,17 @@ export class NextStartInstance extends NextInstance { ...@@ -84,23 +57,17 @@ export class NextStartInstance extends NextInstance {
if (process.env.NEXT_SKIP_ISOLATE) { if (process.env.NEXT_SKIP_ISOLATE) {
// without isolation yarn can't be used and pnpm must be used instead // without isolation yarn can't be used and pnpm must be used instead
if (buildArgs[0] === 'yarn') {
buildArgs[0] = 'pnpm'
}
if (startArgs[0] === 'yarn') { if (startArgs[0] === 'yarn') {
startArgs[0] = 'pnpm' startArgs[0] = 'pnpm'
} }
} }
if (!options.skipBuild) { if (!options.skipBuild) {
const buildArgs = this.getBuildArgs()
console.log('running', buildArgs.join(' ')) console.log('running', buildArgs.join(' '))
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
try { try {
this.childProcess = spawn( this.childProcess = spawn(buildArgs[0], buildArgs.slice(1), spawnOpts)
buildArgs[0],
buildArgs.slice(1),
this.spawnOpts
)
this.handleStdio(this.childProcess) this.handleStdio(this.childProcess)
this.childProcess.on('exit', (code, signal) => { this.childProcess.on('exit', (code, signal) => {
this.childProcess = undefined this.childProcess = undefined
...@@ -135,11 +102,7 @@ export class NextStartInstance extends NextInstance { ...@@ -135,11 +102,7 @@ export class NextStartInstance extends NextInstance {
console.log('running', startArgs.join(' ')) console.log('running', startArgs.join(' '))
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
try { try {
this.childProcess = spawn( this.childProcess = spawn(startArgs[0], startArgs.slice(1), spawnOpts)
startArgs[0],
startArgs.slice(1),
this.spawnOpts
)
this.handleStdio(this.childProcess) this.handleStdio(this.childProcess)
this.childProcess.on('close', (code, signal) => { this.childProcess.on('close', (code, signal) => {
...@@ -184,50 +147,69 @@ export class NextStartInstance extends NextInstance { ...@@ -184,50 +147,69 @@ export class NextStartInstance extends NextInstance {
}) })
} }
public async build( private getBuildArgs(args?: string[]) {
options: { env?: Record<string, string>; args?: string[] } = {} let buildArgs = ['pnpm', 'next', 'build']
) {
this.spawnOpts = { if (this.buildCommand) {
buildArgs = this.buildCommand.split(' ')
}
if (this.buildArgs) {
buildArgs.push(...this.buildArgs)
}
if (args) {
buildArgs.push(...args)
}
if (process.env.NEXT_SKIP_ISOLATE) {
// without isolation yarn can't be used and pnpm must be used instead
if (buildArgs[0] === 'yarn') {
buildArgs[0] = 'pnpm'
}
}
return buildArgs
}
private getSpawnOpts(
env?: Record<string, string>
): import('child_process').SpawnOptions {
return {
cwd: this.testDir, cwd: this.testDir,
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', 'pipe', 'pipe'],
shell: false, shell: false,
env: { env: {
...process.env, ...process.env,
...this.env, ...this.env,
...options.env, ...env,
NODE_ENV: '' as any, NODE_ENV: this.env.NODE_ENV || ('' as any),
PORT: this.forcedPort || '0', PORT: this.forcedPort ?? '0',
__NEXT_TEST_MODE: 'e2e', __NEXT_TEST_MODE: 'e2e',
}, },
} }
}
public async build(
options: { env?: Record<string, string>; args?: string[] } = {}
) {
if (this.childProcess) {
throw new Error(
`can not run export while server is running, use next.stop() first`
)
}
return new Promise<{ return new Promise<{
exitCode: NodeJS.Signals | number | null exitCode: NodeJS.Signals | number | null
cliOutput: string cliOutput: string
}>((resolve) => { }>((resolve) => {
const curOutput = this._cliOutput.length const curOutput = this._cliOutput.length
const buildArgs = ['pnpm', 'next', 'build'] const spawnOpts = this.getSpawnOpts(options.env)
const buildArgs = this.getBuildArgs(options.args)
if (this.buildArgs) {
buildArgs.push(...this.buildArgs)
}
if (options.args) {
buildArgs.push(...options.args)
}
if (this.childProcess) {
throw new Error(
`can not run export while server is running, use next.stop() first`
)
}
console.log('running', buildArgs.join(' ')) console.log('running', buildArgs.join(' '))
this.childProcess = spawn( this.childProcess = spawn(buildArgs[0], buildArgs.slice(1), spawnOpts)
buildArgs[0],
buildArgs.slice(1),
this.spawnOpts
)
this.handleStdio(this.childProcess) this.handleStdio(this.childProcess)
this.childProcess.on('exit', (code, signal) => { this.childProcess.on('exit', (code, signal) => {
......
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