Skip to content

Commit 939e0cd

Browse files
committed
fix(e2e): Fix nextjs-15-spotlight test by using explicit spotlight passing
In Next.js 15, Turbopack is the default dev bundler and doesn't replace process.env vars in node_modules code. This prevents the SDK's auto-detection from working, so we use a workaround: explicitly reading the env var in user code (where Next.js DOES replace it) and passing it to Sentry.init(). Note: Auto-detection DOES work in: - Next.js with webpack (older versions) - Other frameworks like Vite, Create React App, etc. Also filters env vars in playwright-config to prevent unintended side effects from the Spotlight environment variable changes.
1 parent 5bc01a0 commit 939e0cd

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

dev-packages/e2e-tests/test-applications/nextjs-15-spotlight/instrumentation-client.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import * as Sentry from '@sentry/nextjs';
22

3-
// In Next.js 15 Turbopack dev mode, custom loaders aren't applied to instrumentation files.
4-
// So we need to explicitly pass the spotlight value from process.env which Next.js DOES replace.
5-
// This is a workaround for the valueInjectionLoader not working in Turbopack.
3+
// In Next.js 15, Turbopack is the default dev bundler. Turbopack doesn't replace
4+
// process.env vars in node_modules code, which prevents the SDK's auto-detection
5+
// from working. As a workaround, we explicitly pass the spotlight value from
6+
// process.env which Next.js DOES replace in user code.
7+
//
8+
// Note: Auto-detection DOES work in:
9+
// - Next.js with webpack (older versions or configured to use webpack)
10+
// - Other frameworks like Vite, Create React App, etc.
611
const spotlightValue = process.env.NEXT_PUBLIC_SENTRY_SPOTLIGHT === 'true';
712

813
console.log('[Sentry Debug] Spotlight from process.env:', spotlightValue);

dev-packages/e2e-tests/test-applications/nextjs-15-spotlight/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"dev": "next dev -p 3030",
77
"build": "next build",
8+
"start": "next start -p 3030",
89
"test": "playwright test",
910
"test:build": "pnpm install",
1011
"test:assert": "pnpm test"

dev-packages/e2e-tests/test-applications/nextjs-15-spotlight/playwright.config.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { getPlaywrightConfig } from '@sentry-internal/test-utils';
22

33
const config = getPlaywrightConfig({
4-
// Use next dev to test development-mode behavior where Spotlight is auto-enabled
4+
// Use next dev to test development-mode behavior where Spotlight is auto-enabled.
5+
// Note: In Next.js 15, Turbopack is the default dev bundler and doesn't replace
6+
// process.env vars in node_modules code. This prevents the SDK's auto-detection
7+
// from working, so the test explicitly passes the spotlight value.
58
startCommand: 'pnpm dev',
69
port: 3030,
710
eventProxyFile: 'start-event-proxy.mjs',

dev-packages/test-utils/src/playwright-config.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ export function getPlaywrightConfig(
8282
};
8383

8484
if (startCommand) {
85+
// Filter environment variables to only include those that are safe and needed
86+
// We avoid passing ALL env vars to prevent unintended side effects in tests
87+
const filteredEnv = Object.fromEntries(
88+
Object.entries(process.env).filter(([key]) => {
89+
// Include E2E test DSN variables (needed by all test apps)
90+
if (key.includes('E2E_TEST_DSN') || key === 'E2E_TEST_SENTRY_ORG_SLUG' || key === 'E2E_TEST_SENTRY_PROJECT') {
91+
return true;
92+
}
93+
// Include Spotlight environment variables (needed for Spotlight tests)
94+
if (key.includes('SENTRY_SPOTLIGHT')) {
95+
return true;
96+
}
97+
// Include Node/npm related variables for proper execution
98+
if (key === 'PATH' || key === 'NODE_ENV' || key === 'HOME' || key === 'USER') {
99+
return true;
100+
}
101+
// Include CI variable for proper test configuration
102+
if (key === 'CI') {
103+
return true;
104+
}
105+
return false;
106+
}),
107+
);
108+
85109
// @ts-expect-error - we set `config.webserver` to an array above.
86110
// TS just can't infer that and thinks it could also be undefined or an object.
87111
config.webServer.push({
@@ -90,9 +114,7 @@ export function getPlaywrightConfig(
90114
stdout: 'pipe',
91115
stderr: 'pipe',
92116
env: {
93-
// Inherit all environment variables from the parent process
94-
// This is needed for env vars like NEXT_PUBLIC_SENTRY_SPOTLIGHT to be passed through
95-
...process.env,
117+
...filteredEnv,
96118
PORT: appPort.toString(),
97119
},
98120
});

0 commit comments

Comments
 (0)