Unverified Commit 906dbd0a authored by Josh Story's avatar Josh Story Committed by GitHub

test: honor skipStart for Vercel deployments (#98776)

## Summary

Make deploy mode follow the existing setup/start lifecycle: skipStart
prepares the fixture without deploying, and next.start() performs the
deployment. Failed starts retain CLI and build logs before rejecting, so
tests can assert expected build failures in their own bodies. Default
automatic startup still fails its setup hook on an uncaught deployment
error.

Remove the expectDeploymentFailure option. Refactor
app-invalid-revalidate into fixed fixtures and use the same start/error
assertions locally and on Vercel, without a deployment exclusion.

## Verification

- All four invalid-revalidate cases passed against real Vercel preview
builds with Turbopack. Each test caught the rejection from
`next.start()` and asserted the expected diagnostic in `next.cliOutput`;
no skipped tests.
- All four cases passed locally in dev and start modes with both
Turbopack and webpack.
- Seventeen lifecycle unit tests passed, covering the real
`nextTestSetup` hooks, deferred/default startup, failed-build logs,
retries, repeated starts, custom scripts, existing deployments, and
cleanup.
- `pnpm test-deploy-turbo
test/e2e/test-utils-tests/basic/basic.test.ts`: the default automatic
deployment reached READY and loaded its build/deployment IDs, but the
HTTP assertion failed because the temporary project's protection
redirected to Vercel SSO. Project protection settings were left
unchanged.

The suite's separate, existing Cache Components manifest exclusion is
unchanged.

<!-- NEXT_JS_LLM -->
parent 9a20fce8
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils' import { retry } from 'next-test-utils'
import path from 'path'
// TODO(deploy-test-completion): Re-enable this suite in deploy mode.
// It likely asserts local CLI or runtime output that deploy tests do not expose. describe.each(['layout', 'page', 'fetch', 'unstable-cache'])(
// @force-gate !deploy 'app-invalid-revalidate (%s)',
describe('app-invalid-revalidate', () => { (fixture) => {
const { next, isNextDev } = nextTestSetup({ const { next, isNextDev } = nextTestSetup({
files: __dirname, files: path.join(__dirname, 'fixtures', fixture),
skipStart: true, skipStart: true,
}) nextConfig: {
typescript: {
it('should error properly for invalid revalidate at layout', async () => { ignoreBuildErrors: true,
await next.stop().catch(() => {}) },
const origText = await next.readFile('app/layout.tsx') experimental: {
prerenderEarlyExit: false,
try { },
await next.patchFile( },
'app/layout.tsx', })
origText.replace('// export', 'export')
) it('reports the invalid revalidate value', async () => {
await next.start().catch(() => {}) if (isNextDev) {
await next.start()
await check(async () => { await next.fetch('/')
if (isNextDev) { } else {
await next.fetch('/') await expect(next.start()).rejects.toThrow()
} }
return next.cliOutput
}, /Invalid revalidate value "1" on "\/", must be a non-negative number or false/) await retry(() => {
} finally { expect(next.cliOutput).toMatch(
await next.patchFile('app/layout.tsx', origText) fixture === 'unstable-cache'
} ? /Invalid revalidate value "1" on "unstable_cache/
}) : /Invalid revalidate value "1" on "\/", must be a non-negative number or false/
)
it('should error properly for invalid revalidate at page', async () => { })
await next.stop().catch(() => {}) }, 240_000) // This test includes the build/deployment, not just runtime assertions.
const origText = await next.readFile('app/page.tsx') }
)
try {
await next.patchFile(
'app/page.tsx',
origText.replace('// export', 'export')
)
await next.start().catch(() => {})
await check(async () => {
if (isNextDev) {
await next.fetch('/')
}
return next.cliOutput
}, /Invalid revalidate value "1" on "\/", must be a non-negative number or false/)
} finally {
await next.patchFile('app/page.tsx', origText)
}
})
it('should error properly for invalid revalidate on fetch', async () => {
await next.stop().catch(() => {})
const origText = await next.readFile('app/page.tsx')
try {
await next.patchFile(
'app/page.tsx',
origText.replace('// await', 'await')
)
await next.start().catch(() => {})
await check(async () => {
if (isNextDev) {
await next.fetch('/')
}
return next.cliOutput
}, /Invalid revalidate value "1" on "\/", must be a non-negative number or false/)
} finally {
await next.patchFile('app/page.tsx', origText)
}
})
it('should error properly for invalid revalidate on unstable_cache', async () => {
await next.stop().catch(() => {})
const origText = await next.readFile('app/page.tsx')
try {
await next.patchFile(
'app/page.tsx',
origText.replace('// await unstable', 'await unstable')
)
await next.start().catch(() => {})
await check(async () => {
if (isNextDev) {
await next.fetch('/')
}
return next.cliOutput
}, /Invalid revalidate value "1" on "unstable_cache/)
} finally {
await next.patchFile('app/page.tsx', origText)
}
})
})
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default async function Page() {
await fetch('https://example.vercel.sh', { next: { revalidate: '1' } })
return <p>hello world</p>
}
// export const revalidate = '1' export const revalidate = '1'
export default function Root({ children }: { children: React.ReactNode }) { export default function Root({ children }: { children: React.ReactNode }) {
return ( return (
......
export default function Page() {
return <p>hello world</p>
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export const revalidate = '1'
export default function Page() {
return <p>hello world</p>
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { unstable_cache } from 'next/cache' import { unstable_cache } from 'next/cache'
// export const revalidate = '1'
export default async function Page() { export default async function Page() {
// await fetch('https://example.vercel.sh', { next: { revalidate: '1' } }) await unstable_cache(async () => Date.now(), [], { revalidate: '1' })()
// await unstable_cache(async () => Date.now(), [], { revalidate: '1' })()
return <p>hello world</p> return <p>hello world</p>
} }
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
typescript: {
ignoreBuildErrors: true,
},
experimental: {
prerenderEarlyExit: false,
},
}
module.exports = nextConfig
...@@ -20,6 +20,7 @@ export class NextDeployInstance extends NextInstance { ...@@ -20,6 +20,7 @@ export class NextDeployInstance extends NextInstance {
private _supportsImmutableAssets: boolean = false private _supportsImmutableAssets: boolean = false
private _writtenHostsLine: string | null = null private _writtenHostsLine: string | null = null
private _restoreDnsLookup: (() => void) | null = null private _restoreDnsLookup: (() => void) | null = null
private _startPromise: Promise<void> | undefined
constructor(opts: NextInstanceOpts) { constructor(opts: NextInstanceOpts) {
super(opts) super(opts)
...@@ -78,16 +79,19 @@ export class NextDeployInstance extends NextInstance { ...@@ -78,16 +79,19 @@ export class NextDeployInstance extends NextInstance {
...this.env, ...this.env,
} }
const deployRes = await execa(deployScriptPath, [], { const deployment = execa(deployScriptPath, [], {
cwd: this.testDir, cwd: this.testDir,
env: scriptEnv, env: scriptEnv,
reject: false, reject: false,
stderr: 'inherit',
}) })
deployment.stderr?.pipe(process.stderr)
const deployRes = await deployment
if (deployRes.exitCode !== 0) { if (deployRes.exitCode !== 0) {
throw new Error( await this.throwDeploymentError(
`Custom deploy script failed: ${deployRes.stdout} ${deployRes.stderr} (${deployRes.exitCode})` deployRes,
() => this.fetchBuildLogsUsingCustomScript(),
'Custom deploy script failed'
) )
} }
...@@ -111,6 +115,31 @@ export class NextDeployInstance extends NextInstance { ...@@ -111,6 +115,31 @@ export class NextDeployInstance extends NextInstance {
return { url } return { url }
} }
private async throwDeploymentError(
result: { exitCode: number; stdout: string; stderr?: string },
fetchBuildLogs: () => Promise<string>,
message: string
): Promise<never> {
this._cliOutput = result.stdout + (result.stderr || '')
const error = new Error(
`${message}: ${this._cliOutput} (${result.exitCode})`
)
try {
// Upload/authentication failures may not have a deployment URL. Retain
// their CLI output even when there are no remote build logs to fetch.
this._parsedUrl = new URL(result.stdout.trim())
this._url = this._parsedUrl.href
this._cliOutput += await fetchBuildLogs()
} catch (cause) {
error.cause = cause
}
// Preserve the instance and its logs for callers that catch start().
// Failed builds do not produce the successful-build ID markers.
throw error
}
private async fetchBuildLogsUsingCustomScript(): Promise<string> { private async fetchBuildLogsUsingCustomScript(): Promise<string> {
const logsScriptPath = process.env.NEXT_TEST_DEPLOY_LOGS_SCRIPT_PATH! const logsScriptPath = process.env.NEXT_TEST_DEPLOY_LOGS_SCRIPT_PATH!
...@@ -253,11 +282,21 @@ export class NextDeployInstance extends NextInstance { ...@@ -253,11 +282,21 @@ export class NextDeployInstance extends NextInstance {
} }
public async setup(parentSpan: Span) { public async setup(parentSpan: Span) {
super.setup(parentSpan) await super.setup(parentSpan)
await super.createTestDir({ parentSpan, skipInstall: true }) await super.createTestDir({ parentSpan, skipInstall: true })
await this.writeMirrorNpmrcIfNecessary() await this.writeMirrorNpmrcIfNecessary()
if (
!process.env.NEXT_TEST_DEPLOY_URL?.trim() &&
!process.env.NEXT_TEST_DEPLOY_SCRIPT_PATH?.trim() &&
!process.env.NEXT_TEST_VERSION
) {
await this.prepareLocalPackages(parentSpan)
}
}
private async deploy() {
const existingDeployUrl = process.env.NEXT_TEST_DEPLOY_URL?.trim() const existingDeployUrl = process.env.NEXT_TEST_DEPLOY_URL?.trim()
const customDeployScriptPath = const customDeployScriptPath =
process.env.NEXT_TEST_DEPLOY_SCRIPT_PATH?.trim() process.env.NEXT_TEST_DEPLOY_SCRIPT_PATH?.trim()
...@@ -294,12 +333,12 @@ export class NextDeployInstance extends NextInstance { ...@@ -294,12 +333,12 @@ export class NextDeployInstance extends NextInstance {
reject: false, reject: false,
} }
) )
this._cliOutput = buildLogs.stdout + buildLogs.stderr
if (buildLogs.exitCode !== 0) { if (buildLogs.exitCode !== 0) {
throw new Error( throw new Error(
`Failed to get build output logs: ${buildLogs.stderr}` `Failed to get build output logs: ${buildLogs.stderr}`
) )
} }
this._cliOutput = buildLogs.stdout + buildLogs.stderr
} }
this.parseIdsFromCliOutput() this.parseIdsFromCliOutput()
...@@ -330,10 +369,6 @@ export class NextDeployInstance extends NextInstance { ...@@ -330,10 +369,6 @@ export class NextDeployInstance extends NextInstance {
return return
} }
if (!process.env.NEXT_TEST_VERSION) {
await this.prepareLocalPackages(parentSpan)
}
// Original Vercel CLI deployment logic // Original Vercel CLI deployment logic
// ensure Vercel CLI is installed // ensure Vercel CLI is installed
try { try {
...@@ -478,7 +513,7 @@ export class NextDeployInstance extends NextInstance { ...@@ -478,7 +513,7 @@ export class NextDeployInstance extends NextInstance {
additionalEnv.push(`NEXT_ENABLE_ADAPTER=0`) additionalEnv.push(`NEXT_ENABLE_ADAPTER=0`)
} }
const deployRes = await execa( const deployment = execa(
'vercel', 'vercel',
[ [
'deploy', 'deploy',
...@@ -501,16 +536,28 @@ export class NextDeployInstance extends NextInstance { ...@@ -501,16 +536,28 @@ export class NextDeployInstance extends NextInstance {
cwd: this.testDir, cwd: this.testDir,
env: vercelEnv, env: vercelEnv,
reject: false, reject: false,
// This will print deployment information earlier to the console so we
// don't have to wait until the deployment is complete to get the
// inspect URL.
stderr: 'inherit',
} }
) )
// Keep showing deployment progress while also retaining failure output.
deployment.stderr?.pipe(process.stderr)
const deployRes = await deployment
if (deployRes.exitCode !== 0) { if (deployRes.exitCode !== 0) {
throw new Error( await this.throwDeploymentError(
`Failed to deploy project ${deployRes.stdout} ${deployRes.stderr} (${deployRes.exitCode})` deployRes,
async () => {
const logs = await execa(
'vercel',
['inspect', '--logs', this._url, ...vercelFlags],
{ env: vercelEnv, reject: false }
)
// inspect exits 1 for a failed deployment even when logs are returned.
if (logs.exitCode !== 0 && logs.exitCode !== 1) {
throw new Error(`Failed to get build output logs: ${logs.stderr}`)
}
return logs.stdout + logs.stderr
},
'Failed to deploy project'
) )
} }
...@@ -929,7 +976,7 @@ export class NextDeployInstance extends NextInstance { ...@@ -929,7 +976,7 @@ export class NextDeployInstance extends NextInstance {
// Run custom cleanup script if provided // Run custom cleanup script if provided
const customCleanupScriptPath = const customCleanupScriptPath =
process.env.NEXT_TEST_CLEANUP_SCRIPT_PATH?.trim() process.env.NEXT_TEST_CLEANUP_SCRIPT_PATH?.trim()
if (customCleanupScriptPath) { if (customCleanupScriptPath && this._url) {
await this.cleanupUsingCustomScript().catch((err) => { await this.cleanupUsingCustomScript().catch((err) => {
require('console').error( require('console').error(
'Error running custom cleanup script, continuing with destroy:', 'Error running custom cleanup script, continuing with destroy:',
...@@ -978,7 +1025,18 @@ export class NextDeployInstance extends NextInstance { ...@@ -978,7 +1025,18 @@ export class NextDeployInstance extends NextInstance {
} }
public async start() { public async start() {
// no-op as the deployment is created during setup() this.throwIfUnavailable()
if (!this._startPromise) {
this._cliOutput = ''
this._url = ''
// Reuse a ready deployment on subsequent calls, as start() did before
// deployment moved out of setup(). A failed attempt can be retried.
this._startPromise = this.deploy().catch((error) => {
this._startPromise = undefined
throw error
})
}
await this._startPromise
} }
public async patchFile( public async patchFile(
......
This diff is collapsed.
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