With `experimental.testProxy` on, `@mswjs/interceptors` (switched to
socket-level interception in #96059) also catches the passthrough
`fetch` that `handleFetch` makes for non-test requests. That request has
no `next-test-internal` marker, so the interceptor calls `handleFetch`
again, takes the passthrough branch again, and loops until it errors. So
any server-side request made outside a test context never resolves
during a test run.
This adds the `next-test-internal` marker to the passthrough request,
and to the `continue` case that has the same problem. It is the guard
#96059 already added for the proxy-protocol request, so the request
reaches the real server instead of being intercepted again.
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: Sam Ko <sam@vercel.com>