Unverified Commit 61384522 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

[test]: add ability to run test-deploy with pre-existing deployment (#88829)

When digging into failing deployment tests, I often will re-run the test
itself without wanting to change anything about Next.js or the test
application, especially when I suspect a logic bug in the test itself or
need to figure out what's causing a flake.

This adds an environment variable that can be used to skip the
build/deploy step and will use an existing deployment URL to run the
tests against.

I suggest reviewing this [without
whitespace](https://github.com/vercel/next.js/pull/88829/changes?w=1).
parent d2ea7d32
...@@ -92,6 +92,8 @@ these can be leveraged by prefixing the `pnpm test` command. ...@@ -92,6 +92,8 @@ these can be leveraged by prefixing the `pnpm test` command.
it can be used when not using `pnpm test-dev` or `pnpm test-start` directly. it can be used when not using `pnpm test-dev` or `pnpm test-start` directly.
Valid test modes can be seen here: Valid test modes can be seen here:
https://github.com/vercel/next.js/blob/aa664868c102ddc5adc618415162d124503ad12e/test/lib/e2e-utils.ts#L46 https://github.com/vercel/next.js/blob/aa664868c102ddc5adc618415162d124503ad12e/test/lib/e2e-utils.ts#L46
- Use `NEXT_TEST_DEPLOY_URL` with `pnpm test-deploy` to skip the Vercel deploy step and run
deploy-mode assertions against an existing deployment URL.
- You can use `NEXT_TEST_PREFER_OFFLINE=1` while testing to configure the package manager to include the - You can use `NEXT_TEST_PREFER_OFFLINE=1` while testing to configure the package manager to include the
[`--prefer-offline`](https://pnpm.io/cli/install#--prefer-offline) argument during test setup. [`--prefer-offline`](https://pnpm.io/cli/install#--prefer-offline) argument during test setup.
This is helpful when running tests in internet-restricted environments such as planes or public Wi-Fi. This is helpful when running tests in internet-restricted environments such as planes or public Wi-Fi.
......
...@@ -42,6 +42,8 @@ export class NextDeployInstance extends NextInstance { ...@@ -42,6 +42,8 @@ export class NextDeployInstance extends NextInstance {
super.setup(parentSpan) super.setup(parentSpan)
await super.createTestDir({ parentSpan, skipInstall: true }) await super.createTestDir({ parentSpan, skipInstall: true })
const existingDeployUrl = process.env.NEXT_TEST_DEPLOY_URL?.trim()
// ensure Vercel CLI is installed // ensure Vercel CLI is installed
try { try {
const res = await execa('vercel', ['--version']) const res = await execa('vercel', ['--version'])
...@@ -84,89 +86,101 @@ export class NextDeployInstance extends NextInstance { ...@@ -84,89 +86,101 @@ export class NextDeployInstance extends NextInstance {
) )
vercelFlags.push('--global-config', vcConfigDir) vercelFlags.push('--global-config', vcConfigDir)
} }
require('console').log(`Linking project at ${this.testDir}`) if (existingDeployUrl) {
try {
// link the project this._url = new URL(existingDeployUrl).toString()
const linkRes = await execa( } catch (err) {
'vercel', throw new Error(
['link', '-p', TEST_PROJECT_NAME, '--yes', ...vercelFlags], `Invalid NEXT_TEST_DEPLOY_URL value: ${existingDeployUrl}`,
{ { cause: err }
cwd: this.testDir, )
env: vercelEnv,
reject: false,
} }
) require('console').log(`Using existing deployment URL: ${this._url}`)
} else {
if (linkRes.exitCode !== 0) { require('console').log(`Linking project at ${this.testDir}`)
throw new Error(
`Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})` // link the project
const linkRes = await execa(
'vercel',
['link', '-p', TEST_PROJECT_NAME, '--yes', ...vercelFlags],
{
cwd: this.testDir,
env: vercelEnv,
reject: false,
}
) )
}
require('console').log(`Deploying project at ${this.testDir}`)
const additionalEnv: string[] = []
for (const key of Object.keys(this.env || {})) { if (linkRes.exitCode !== 0) {
additionalEnv.push(`${key}=${this.env[key]}`) throw new Error(
} `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
)
}
require('console').log(`Deploying project at ${this.testDir}`)
additionalEnv.push( const additionalEnv: string[] = []
`VERCEL_CLI_VERSION=${process.env.VERCEL_CLI_VERSION || 'vercel@latest'}`
)
// Add experimental feature flags for (const key of Object.keys(this.env || {})) {
additionalEnv.push(`${key}=${this.env[key]}`)
}
if (process.env.__NEXT_CACHE_COMPONENTS) {
additionalEnv.push( additionalEnv.push(
`NEXT_PRIVATE_EXPERIMENTAL_CACHE_COMPONENTS=${process.env.__NEXT_CACHE_COMPONENTS}` `VERCEL_CLI_VERSION=${process.env.VERCEL_CLI_VERSION || 'vercel@latest'}`
) )
}
if (process.env.IS_TURBOPACK_TEST) { // Add experimental feature flags
additionalEnv.push(`IS_TURBOPACK_TEST=1`)
}
if (process.env.IS_WEBPACK_TEST) {
additionalEnv.push(`IS_WEBPACK_TEST=1`)
}
const deployRes = await execa( if (process.env.__NEXT_CACHE_COMPONENTS) {
'vercel', additionalEnv.push(
[ `NEXT_PRIVATE_EXPERIMENTAL_CACHE_COMPONENTS=${process.env.__NEXT_CACHE_COMPONENTS}`
'deploy', )
'--build-env', }
'NEXT_PRIVATE_TEST_MODE=e2e',
'--build-env', if (process.env.IS_TURBOPACK_TEST) {
'NEXT_TELEMETRY_DISABLED=1', additionalEnv.push(`IS_TURBOPACK_TEST=1`)
'--build-env', }
'VERCEL_NEXT_BUNDLED_SERVER=1', if (process.env.IS_WEBPACK_TEST) {
...additionalEnv.flatMap((pair) => [ additionalEnv.push(`IS_WEBPACK_TEST=1`)
'--env',
pair,
'--build-env',
pair,
]),
'--force',
...vercelFlags,
],
{
cwd: this.testDir,
env: vercelEnv,
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',
} }
)
if (deployRes.exitCode !== 0) { const deployRes = await execa(
throw new Error( 'vercel',
`Failed to deploy project ${deployRes.stdout} ${deployRes.stderr} (${deployRes.exitCode})` [
'deploy',
'--build-env',
'NEXT_PRIVATE_TEST_MODE=e2e',
'--build-env',
'NEXT_TELEMETRY_DISABLED=1',
'--build-env',
'VERCEL_NEXT_BUNDLED_SERVER=1',
...additionalEnv.flatMap((pair) => [
'--env',
pair,
'--build-env',
pair,
]),
'--force',
...vercelFlags,
],
{
cwd: this.testDir,
env: vercelEnv,
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',
}
) )
}
// the CLI gives just the deployment URL back when not a TTY if (deployRes.exitCode !== 0) {
this._url = deployRes.stdout throw new Error(
`Failed to deploy project ${deployRes.stdout} ${deployRes.stderr} (${deployRes.exitCode})`
)
}
// the CLI gives just the deployment URL back when not a TTY
this._url = deployRes.stdout
}
this._parsedUrl = new URL(this._url) this._parsedUrl = new URL(this._url)
// If configured, we should configure the `/etc/hosts` file to point the // If configured, we should configure the `/etc/hosts` file to point the
......
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