Unverified Commit dfb6e044 authored by Jamiboy Mohammad's avatar Jamiboy Mohammad Committed by GitHub

test: extract a shared helper for deployment feature flags (#99533)

## Summary

Extract the root deploy harness's Cache Components flag capture into a
dependency-free helper that can also be used when preparing nested app
configs. The helper owns a static flag list and emits locally captured
JavaScript assignments; it requires no import from the deployed fixture.
The root config keeps its existing assignment position and behavior.

## Verification

- Generated assignments were executed in isolated contexts to verify
enabled/disabled values, missing and empty values, string escaping,
capture timing, and unrelated environment preservation. Together with
the existing deployment lifecycle tests: 27/27 unit tests passed.
- A focused real Vercel deployment of
`test/e2e/test-utils-tests/basic/basic.test.ts` passed against
`16.4.0-canary.55` with `__NEXT_CACHE_COMPONENTS=true`. The remote build
explicitly reported Cache Components enabled.
- Full bootstrap (18/18 tasks), repository TypeScript checking,
formatting, and ESLint passed.

<!-- NEXT_JS_LLM -->
parent c24d0f87
const deploymentTestFlags = ['__NEXT_CACHE_COMPONENTS']
/**
* Capture test flags in the deployed config because Vercel environment variable
* names cannot start with an underscore. This runs in the local test harness.
*/
export function getDeploymentTestEnvAssignments(): string {
let assignments = ''
for (const flag of deploymentTestFlags) {
const value = process.env[flag]
if (value) {
assignments += `process.env.${flag} = ${JSON.stringify(value)}\n`
}
}
return assignments
}
...@@ -4,6 +4,7 @@ import { existsSync, promises as fs, rmSync, readFileSync } from 'fs' ...@@ -4,6 +4,7 @@ import { existsSync, promises as fs, rmSync, readFileSync } from 'fs'
import treeKill from 'tree-kill' import treeKill from 'tree-kill'
import type { NextConfig } from 'next' import type { NextConfig } from 'next'
import { FileRef, isNextDeploy, PatchedFileRef } from '../e2e-utils' import { FileRef, isNextDeploy, PatchedFileRef } from '../e2e-utils'
import { getDeploymentTestEnvAssignments } from '../e2e-utils/deployment-test-env'
import { ChildProcess } from 'child_process' import { ChildProcess } from 'child_process'
import spawn from 'cross-spawn' import spawn from 'cross-spawn'
import { quote as shellQuote } from 'shell-quote' import { quote as shellQuote } from 'shell-quote'
...@@ -538,11 +539,7 @@ export class NextInstance { ...@@ -538,11 +539,7 @@ export class NextInstance {
await fs.writeFile( await fs.writeFile(
fileName, fileName,
`${content}\n` + `${content}\n` +
// Capture the test flag in the config so it does not need a getDeploymentTestEnvAssignments() +
// deployment-compatible environment variable alias.
(process.env.__NEXT_CACHE_COMPONENTS
? `process.env.__NEXT_CACHE_COMPONENTS = ${JSON.stringify(process.env.__NEXT_CACHE_COMPONENTS)}\n`
: '') +
` `
// alias __NEXT_TEST_MODE for next-deploy as "_" is not a valid // alias __NEXT_TEST_MODE for next-deploy as "_" is not a valid
// env variable during deploy // env variable during deploy
......
import { runInNewContext } from 'vm'
import { getDeploymentTestEnvAssignments } from '../../lib/e2e-utils/deployment-test-env'
describe.each(['__NEXT_CACHE_COMPONENTS'])(
'deployment capture of %s',
(flag) => {
beforeEach(() => {
jest.replaceProperty(process, 'env', {
...process.env,
__NEXT_CACHE_COMPONENTS: '',
})
})
afterEach(() => {
jest.restoreAllMocks()
})
it.each(['true', 'false', '0', '"; process.env.INJECTED = "yes"; //\n\\'])(
'restores the literal value %j in the deployed process',
(value) => {
process.env[flag] = value
const env: Record<string, string> = {}
runInNewContext(getDeploymentTestEnvAssignments(), { process: { env } })
expect(env).toEqual({ [flag]: value })
}
)
it.each(['', undefined])(
'leaves the deployed environment unchanged when the local flag is %j',
(value) => {
if (value === undefined) delete process.env[flag]
else process.env[flag] = value
const env = { [flag]: 'deployment-value' }
runInNewContext(getDeploymentTestEnvAssignments(), { process: { env } })
expect(env).toEqual({ [flag]: 'deployment-value' })
}
)
it('captures the value when generating the config, without needing a remote alias', () => {
process.env[flag] = 'true'
const assignments = getDeploymentTestEnvAssignments()
process.env[flag] = 'false'
const env: Record<string, string> = {}
runInNewContext(assignments, { process: { env } })
expect(env[flag]).toBe('true')
})
}
)
it('does not copy unrelated environment variables into the config', () => {
jest.replaceProperty(process, 'env', {
...process.env,
__NEXT_CACHE_COMPONENTS: 'true',
UNRELATED_TEST_ENV: 'local-value',
})
try {
const env = { UNRELATED_TEST_ENV: 'deployment-value' }
runInNewContext(getDeploymentTestEnvAssignments(), { process: { env } })
expect(env.UNRELATED_TEST_ENV).toBe('deployment-value')
} finally {
jest.restoreAllMocks()
}
})
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