Unverified Commit 4c467a26 authored by Andrew Gadzik's avatar Andrew Gadzik Committed by GitHub

Improve experimental test proxy (#63567)

Followup on https://github.com/vercel/next.js/pull/52520 and
https://github.com/vercel/next.js/pull/54014

**Enhancements**
- Removes `--experimental-test-proxy` CLI argument from `next dev` and
`next start`
- Adds a new experimental config option `testProxy?: boolean`
- Instead of throwing an error, return the `originalFetch` response if
the current request context does not contain the `Next-Test-*` HTTP
headers

**Why?**
These changes allow us to write mixed Integration + E2E tests within the
same Playwright process.

```ts
// some-page.spec.ts

test.describe('/some-page', () => {
	test('some integration test', async ({ page, next }) => {
	  // by using the `next` fixture, playwright will send the `Next-Test-*` HTTP headers for 
	  // every request in this test's context.
	  next.onFetch(...);
	  
	  await page.goto(...);
	  await expect(...).toBe('some-mocked-value');
	});

	test('some e2e test', async ({ page }) => {
	  // by NOT using the `next` fixture, playwright does not send the `Next-Test-*` HTTP headers
	  await page.goto(...);
	  await expect(...).toBe('some-real-value');
	});
})
```

Now I can run `next dev` and locally develop my App Router pages AND run
my Playwright tests against instead of having to,
- run `next dev` to locally develop my change
- ctrl+c to kill server
- run `next dev --experimental-test-proxy` to locally run my integration
tests

---------
Co-authored-by: 's avatarSam Ko <sam@vercel.com>
parent 7de7cbf2
...@@ -169,7 +169,6 @@ program ...@@ -169,7 +169,6 @@ program
'--experimental-upload-trace, <traceUrl>', '--experimental-upload-trace, <traceUrl>',
'Reports a subset of the debugging trace to a remote HTTP URL. Includes sensitive data.' 'Reports a subset of the debugging trace to a remote HTTP URL. Includes sensitive data.'
) )
.addOption(new Option('--experimental-test-proxy').hideHelp())
.action((directory, options, { _optionValueSources }) => { .action((directory, options, { _optionValueSources }) => {
const portSource = _optionValueSources.port const portSource = _optionValueSources.port
import('../cli/next-dev.js').then((mod) => import('../cli/next-dev.js').then((mod) =>
...@@ -313,7 +312,6 @@ program ...@@ -313,7 +312,6 @@ program
'Specify the maximum amount of milliseconds to wait before closing inactive connections.' 'Specify the maximum amount of milliseconds to wait before closing inactive connections.'
).argParser(myParseInt) ).argParser(myParseInt)
) )
.addOption(new Option('--experimental-test-proxy').hideHelp())
.action((directory, options) => .action((directory, options) =>
import('../cli/next-start.js').then((mod) => import('../cli/next-start.js').then((mod) =>
mod.nextStart(options, directory) mod.nextStart(options, directory)
......
...@@ -43,7 +43,6 @@ type NextDevOptions = { ...@@ -43,7 +43,6 @@ type NextDevOptions = {
experimentalHttpsCert?: string experimentalHttpsCert?: string
experimentalHttpsCa?: string experimentalHttpsCa?: string
experimentalUploadTrace?: string experimentalUploadTrace?: string
experimentalTestProxy?: boolean
} }
type PortSource = 'cli' | 'default' | 'env' type PortSource = 'cli' | 'default' | 'env'
...@@ -194,8 +193,6 @@ const nextDev = async ( ...@@ -194,8 +193,6 @@ const nextDev = async (
config = await loadConfig(PHASE_DEVELOPMENT_SERVER, dir) config = await loadConfig(PHASE_DEVELOPMENT_SERVER, dir)
const isExperimentalTestProxy = options.experimentalTestProxy
if ( if (
options.experimentalUploadTrace && options.experimentalUploadTrace &&
!process.env.NEXT_TRACE_UPLOAD_DISABLED !process.env.NEXT_TRACE_UPLOAD_DISABLED
...@@ -216,7 +213,6 @@ const nextDev = async ( ...@@ -216,7 +213,6 @@ const nextDev = async (
allowRetry, allowRetry,
isDev: true, isDev: true,
hostname: host, hostname: host,
isExperimentalTestProxy,
} }
if (options.turbo) { if (options.turbo) {
......
...@@ -13,14 +13,12 @@ type NextStartOptions = { ...@@ -13,14 +13,12 @@ type NextStartOptions = {
port: number port: number
hostname?: string hostname?: string
keepAliveTimeout?: number keepAliveTimeout?: number
experimentalTestProxy?: boolean
} }
const nextStart = async (options: NextStartOptions, directory?: string) => { const nextStart = async (options: NextStartOptions, directory?: string) => {
const dir = getProjectDir(directory) const dir = getProjectDir(directory)
const host = options.hostname const host = options.hostname
const port = options.port const port = options.port
const isExperimentalTestProxy = options.experimentalTestProxy
let keepAliveTimeout = options.keepAliveTimeout let keepAliveTimeout = options.keepAliveTimeout
if (isPortIsReserved(port)) { if (isPortIsReserved(port)) {
...@@ -30,7 +28,6 @@ const nextStart = async (options: NextStartOptions, directory?: string) => { ...@@ -30,7 +28,6 @@ const nextStart = async (options: NextStartOptions, directory?: string) => {
await startServer({ await startServer({
dir, dir,
isDev: false, isDev: false,
isExperimentalTestProxy,
hostname: host, hostname: host,
port, port,
keepAliveTimeout, keepAliveTimeout,
......
...@@ -88,7 +88,8 @@ export async function handleFetch( ...@@ -88,7 +88,8 @@ export async function handleFetch(
): Promise<Response> { ): Promise<Response> {
const testInfo = getTestReqInfo(request, reader) const testInfo = getTestReqInfo(request, reader)
if (!testInfo) { if (!testInfo) {
throw new Error(`No test info for ${request.method} ${request.url}`) // Passthrough non-test requests.
return originalFetch(request)
} }
const { testData, proxyPort } = testInfo const { testData, proxyPort } = testInfo
......
...@@ -4,6 +4,16 @@ ...@@ -4,6 +4,16 @@
You have a Next.js project. You have a Next.js project.
### Update `next.config.js`
```javascript
module.exports = {
experimental: {
testProxy: true,
},
}
```
### Install `@playwright/test` in your project ### Install `@playwright/test` in your project
```sh ```sh
...@@ -25,7 +35,7 @@ import { defineConfig } from 'next/experimental/testmode/playwright' ...@@ -25,7 +35,7 @@ import { defineConfig } from 'next/experimental/testmode/playwright'
export default defineConfig({ export default defineConfig({
webServer: { webServer: {
command: 'npm dev -- --experimental-test-proxy', command: 'npm dev',
url: 'http://localhost:3000', url: 'http://localhost:3000',
}, },
}) })
......
...@@ -39,17 +39,14 @@ export const test = base.test.extend< ...@@ -39,17 +39,14 @@ export const test = base.test.extend<
{ scope: 'worker', auto: true }, { scope: 'worker', auto: true },
], ],
next: [ next: async ({ nextOptions, _nextWorker, page }, use, testInfo) => {
async ({ nextOptions, _nextWorker, page }, use, testInfo) => { await applyNextFixture(use, {
await applyNextFixture(use, { testInfo,
testInfo, nextWorker: _nextWorker,
nextWorker: _nextWorker, page,
page, nextOptions,
nextOptions, })
}) },
},
{ auto: true },
],
}) })
export default test export default test
...@@ -382,6 +382,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -382,6 +382,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
useLightningcss: z.boolean().optional(), useLightningcss: z.boolean().optional(),
missingSuspenseWithCSRBailout: z.boolean().optional(), missingSuspenseWithCSRBailout: z.boolean().optional(),
useEarlyImport: z.boolean().optional(), useEarlyImport: z.boolean().optional(),
testProxy: z.boolean().optional(),
}) })
.optional(), .optional(),
exportPathMap: z exportPathMap: z
......
...@@ -427,6 +427,11 @@ export interface ExperimentalConfig { ...@@ -427,6 +427,11 @@ export interface ExperimentalConfig {
* Enables early import feature for app router modules * Enables early import feature for app router modules
*/ */
useEarlyImport?: boolean useEarlyImport?: boolean
/**
* Enables `fetch` requests to be proxied to the experimental text proxy server
*/
testProxy?: boolean
} }
export type ExportPathMap = { export type ExportPathMap = {
......
...@@ -71,7 +71,6 @@ export async function initialize(opts: { ...@@ -71,7 +71,6 @@ export async function initialize(opts: {
isNodeDebugging: boolean isNodeDebugging: boolean
keepAliveTimeout?: number keepAliveTimeout?: number
customServer?: boolean customServer?: boolean
experimentalTestProxy?: boolean
experimentalHttpsServer?: boolean experimentalHttpsServer?: boolean
startServerSpan?: Span startServerSpan?: Span
}): Promise<[WorkerRequestHandler, WorkerUpgradeHandler, NextServer]> { }): Promise<[WorkerRequestHandler, WorkerUpgradeHandler, NextServer]> {
...@@ -579,7 +578,7 @@ export async function initialize(opts: { ...@@ -579,7 +578,7 @@ export async function initialize(opts: {
} }
let requestHandler: WorkerRequestHandler = requestHandlerImpl let requestHandler: WorkerRequestHandler = requestHandlerImpl
if (opts.experimentalTestProxy) { if (config.experimental.testProxy) {
// Intercept fetch and other testmode apis. // Intercept fetch and other testmode apis.
const { const {
wrapRequestHandlerWorker, wrapRequestHandlerWorker,
...@@ -599,7 +598,7 @@ export async function initialize(opts: { ...@@ -599,7 +598,7 @@ export async function initialize(opts: {
server: opts.server, server: opts.server,
isNodeDebugging: !!opts.isNodeDebugging, isNodeDebugging: !!opts.isNodeDebugging,
serverFields: developmentBundler?.serverFields || {}, serverFields: developmentBundler?.serverFields || {},
experimentalTestProxy: !!opts.experimentalTestProxy, experimentalTestProxy: !!config.experimental.testProxy,
experimentalHttpsServer: !!opts.experimentalHttpsServer, experimentalHttpsServer: !!opts.experimentalHttpsServer,
bundlerService: devBundlerService, bundlerService: devBundlerService,
startServerSpan: opts.startServerSpan, startServerSpan: opts.startServerSpan,
......
...@@ -40,7 +40,6 @@ export interface StartServerOptions { ...@@ -40,7 +40,6 @@ export interface StartServerOptions {
keepAliveTimeout?: number keepAliveTimeout?: number
// this is dev-server only // this is dev-server only
selfSignedCertificate?: SelfSignedCertificate selfSignedCertificate?: SelfSignedCertificate
isExperimentalTestProxy?: boolean
} }
export async function getRequestHandlers({ export async function getRequestHandlers({
...@@ -52,7 +51,6 @@ export async function getRequestHandlers({ ...@@ -52,7 +51,6 @@ export async function getRequestHandlers({
minimalMode, minimalMode,
isNodeDebugging, isNodeDebugging,
keepAliveTimeout, keepAliveTimeout,
experimentalTestProxy,
experimentalHttpsServer, experimentalHttpsServer,
}: { }: {
dir: string dir: string
...@@ -63,7 +61,6 @@ export async function getRequestHandlers({ ...@@ -63,7 +61,6 @@ export async function getRequestHandlers({
minimalMode?: boolean minimalMode?: boolean
isNodeDebugging?: boolean isNodeDebugging?: boolean
keepAliveTimeout?: number keepAliveTimeout?: number
experimentalTestProxy?: boolean
experimentalHttpsServer?: boolean experimentalHttpsServer?: boolean
}): ReturnType<typeof initialize> { }): ReturnType<typeof initialize> {
return initialize({ return initialize({
...@@ -75,7 +72,6 @@ export async function getRequestHandlers({ ...@@ -75,7 +72,6 @@ export async function getRequestHandlers({
server, server,
isNodeDebugging: isNodeDebugging || false, isNodeDebugging: isNodeDebugging || false,
keepAliveTimeout, keepAliveTimeout,
experimentalTestProxy,
experimentalHttpsServer, experimentalHttpsServer,
startServerSpan, startServerSpan,
}) })
...@@ -91,7 +87,6 @@ export async function startServer( ...@@ -91,7 +87,6 @@ export async function startServer(
minimalMode, minimalMode,
allowRetry, allowRetry,
keepAliveTimeout, keepAliveTimeout,
isExperimentalTestProxy,
selfSignedCertificate, selfSignedCertificate,
} = serverOptions } = serverOptions
let { port } = serverOptions let { port } = serverOptions
...@@ -302,7 +297,6 @@ export async function startServer( ...@@ -302,7 +297,6 @@ export async function startServer(
minimalMode, minimalMode,
isNodeDebugging: Boolean(nodeDebugType), isNodeDebugging: Boolean(nodeDebugType),
keepAliveTimeout, keepAliveTimeout,
experimentalTestProxy: !!isExperimentalTestProxy,
experimentalHttpsServer: !!selfSignedCertificate, experimentalHttpsServer: !!selfSignedCertificate,
}) })
requestHandler = initResult[0] requestHandler = initResult[0]
......
...@@ -6,6 +6,8 @@ export async function generateMetadata() { ...@@ -6,6 +6,8 @@ export async function generateMetadata() {
} }
export default async function Page() { export default async function Page() {
const text = await (await fetch('https://example.com')).text() const text = await (
await fetch('https://next-data-api-endpoint.vercel.app/api/random')
).text()
return <pre>{text}</pre> return <pre>{text}</pre>
} }
/** @type {import('next').NextConfig} */
module.exports = { module.exports = {
experimental: {
testProxy: true,
},
rewrites() { rewrites() {
return [ return [
{ {
......
...@@ -7,9 +7,6 @@ createNextDescribe( ...@@ -7,9 +7,6 @@ createNextDescribe(
files: __dirname, files: __dirname,
skipDeployment: true, skipDeployment: true,
dependencies: require('./package.json').dependencies, dependencies: require('./package.json').dependencies,
startCommand: (global as any).isNextDev
? 'pnpm next dev --experimental-test-proxy'
: 'pnpm next start --experimental-test-proxy',
}, },
({ next, isNextDev }) => { ({ next, isNextDev }) => {
let proxyServer: Awaited<ReturnType<typeof createProxyServer>> let proxyServer: Awaited<ReturnType<typeof createProxyServer>>
...@@ -19,7 +16,10 @@ createNextDescribe( ...@@ -19,7 +16,10 @@ createNextDescribe(
onFetch: async (testData, request) => { onFetch: async (testData, request) => {
if ( if (
request.method === 'GET' && request.method === 'GET' &&
request.url === 'https://example.com/' [
'https://example.com/',
'https://next-data-api-endpoint.vercel.app/api/random',
].includes(request.url)
) { ) {
return new Response(testData) return new Response(testData)
} }
...@@ -48,6 +48,11 @@ createNextDescribe( ...@@ -48,6 +48,11 @@ createNextDescribe(
} }
describe('app router', () => { describe('app router', () => {
it('should fetch real data when Next-Test-* headers are not present', async () => {
const html = await (await next.fetch('/app/rsc-fetch')).text()
expect(html).not.toContain('<pre>test1</pre>')
})
it('should handle RSC with fetch in serverless function', async () => { it('should handle RSC with fetch in serverless function', async () => {
const html = await (await fetchForTest('/app/rsc-fetch')).text() const html = await (await fetchForTest('/app/rsc-fetch')).text()
expect(html).toContain('<pre>test1</pre>') expect(html).toContain('<pre>test1</pre>')
......
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