-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nuxt): Detect development environment and add dev E2E test #18671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
745bb35
9bbb13c
e463c3d
f582ec2
a35268d
17a2f13
23b90cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #!/bin/bash | ||
| # To enable Sentry in Nuxt dev, it needs the sentry.server.config.mjs file from the .nuxt folder. | ||
| # First, we need to start 'nuxt dev' to generate the file, and then start 'nuxt dev' again with the NODE_OPTIONS to have Sentry enabled. | ||
|
|
||
| # 1. Start dev in background - this generates .nuxt folder | ||
| # Using a different port to avoid playwright already starting with the tests for port 3030 | ||
| pnpm dev -p 3035 & | ||
| DEV_PID=$! | ||
|
|
||
| # 2. Wait for the sentry.server.config.mjs file to appear | ||
| echo "Waiting for .nuxt/dev/sentry.server.config.mjs file..." | ||
| COUNTER=0 | ||
| while [ ! -f ".nuxt/dev/sentry.server.config.mjs" ] && [ $COUNTER -lt 30 ]; do | ||
| sleep 1 | ||
| ((COUNTER++)) | ||
| done | ||
|
|
||
| if [ ! -f ".nuxt/dev/sentry.server.config.mjs" ]; then | ||
| echo "ERROR: .nuxt/dev/sentry.server.config.mjs file never appeared!" | ||
| pkill -P $DEV_PID || kill $DEV_PID || pkill -f "nuxt" | ||
|
||
| exit 1 | ||
| fi | ||
|
|
||
| # 3. Cleanup | ||
| echo "Found .nuxt/dev/sentry.server.config.mjs, stopping 'nuxt dev' process..." | ||
| pkill -P $DEV_PID || kill $DEV_PID || pkill -f "nuxt" | ||
|
||
| sleep 2 # Give it a moment to release ports | ||
|
||
|
|
||
| echo "Starting nuxt dev with Sentry server config..." | ||
|
|
||
| # 4. Start the actual dev command which should be used for the tests | ||
| NODE_OPTIONS='--import ./.nuxt/dev/sentry.server.config.mjs' nuxt dev | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,25 @@ | ||
| import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
|
||
| const testEnv = process.env.TEST_ENV; | ||
|
|
||
| if (!testEnv) { | ||
| throw new Error('No test env defined'); | ||
| } | ||
|
|
||
| const getStartCommand = () => { | ||
| if (testEnv === 'development') { | ||
| return 'bash ./nuxt-start-dev-server.bash'; | ||
| } | ||
|
|
||
| if (testEnv === 'production') { | ||
| return 'pnpm start:import'; | ||
| } | ||
|
|
||
| throw new Error(`Unknown test env: ${testEnv}`); | ||
| }; | ||
|
|
||
| const config = getPlaywrightConfig({ | ||
| startCommand: `pnpm start:import`, | ||
| startCommand: getStartCommand(), | ||
| }); | ||
|
|
||
| export default config; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ import * as Sentry from '@sentry/nuxt'; | |
|
|
||
| Sentry.init({ | ||
| dsn: 'https://[email protected]/1337', | ||
| environment: 'qa', // dynamic sampling bias to keep transactions | ||
| tracesSampleRate: 1.0, // Capture 100% of the transactions | ||
| tunnel: 'http://localhost:3031/', // proxy server | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; | ||
| import { isDevMode } from './isDevMode'; | ||
|
|
||
| test.describe('environment detection', async () => { | ||
| test('sets correct environment for application errors', async ({ page }) => { | ||
| const errorPromise = waitForError('nuxt-4', async errorEvent => { | ||
| return errorEvent?.exception?.values?.[0]?.value === 'Error thrown from Nuxt-4 E2E test app'; | ||
| }); | ||
|
|
||
| // We have to wait for networkidle in dev mode because clicking the button is a no-op otherwise (network requests are blocked during page load) | ||
| await page.goto(`/client-error`, isDevMode ? { waitUntil: 'networkidle' } : {}); | ||
| await page.locator('#errorBtn').click(); | ||
|
|
||
| const error = await errorPromise; | ||
|
|
||
| if (isDevMode) { | ||
| expect(error.environment).toBe('development'); | ||
| } else { | ||
| expect(error.environment).toBe('production'); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests use conditionals instead of separate test paths (Bugbot Rules)Low Severity Flagged per review rules: The tests use Additional Locations (2) |
||
| }); | ||
|
|
||
| test('sets correct environment for application transactions', async ({ page }) => { | ||
| const transactionPromise = waitForTransaction('nuxt-4', async transactionEvent => { | ||
| return transactionEvent.transaction === '/test-param/:param()'; | ||
| }); | ||
|
|
||
| await page.goto(`/test-param/1234`); | ||
|
|
||
| const transaction = await transactionPromise; | ||
|
|
||
| if (isDevMode) { | ||
| expect(transaction.environment).toBe('development'); | ||
| } else { | ||
| expect(transaction.environment).toBe('production'); | ||
| } | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const isDevMode = !!process.env.TEST_ENV && process.env.TEST_ENV.includes('development'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export const DEFAULT_ENVIRONMENT = 'production'; | ||
| export const DEV_ENVIRONMENT = 'development'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,21 @@ | ||
| import * as path from 'node:path'; | ||
| import type { Client, Event, EventProcessor, Integration } from '@sentry/core'; | ||
| import { applySdkMetadata, debug, flush, getGlobalScope, vercelWaitUntil } from '@sentry/core'; | ||
| import { | ||
| applySdkMetadata, | ||
| debug, | ||
| DEFAULT_ENVIRONMENT, | ||
| DEV_ENVIRONMENT, | ||
| flush, | ||
| getGlobalScope, | ||
| vercelWaitUntil, | ||
| } from '@sentry/core'; | ||
| import { | ||
| getDefaultIntegrations as getDefaultNodeIntegrations, | ||
| httpIntegration, | ||
| init as initNode, | ||
| type NodeOptions, | ||
| } from '@sentry/node'; | ||
| import { isCjs } from '@sentry/node-core'; | ||
| import { DEBUG_BUILD } from '../common/debug-build'; | ||
| import type { SentryNuxtServerOptions } from '../common/types'; | ||
|
|
||
|
|
@@ -17,8 +26,9 @@ import type { SentryNuxtServerOptions } from '../common/types'; | |
| */ | ||
| export function init(options: SentryNuxtServerOptions): Client | undefined { | ||
| const sentryOptions = { | ||
| ...options, | ||
| defaultIntegrations: getNuxtDefaultIntegrations(options), | ||
| environment: !isCjs() && import.meta.dev ? DEV_ENVIRONMENT : DEFAULT_ENVIRONMENT, | ||
This comment was marked as outdated.
Sorry, something went wrong.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good point, is this actually available in init when we The tests on CI seem to pass, I wonder if that's because we run the dev command twice? Could you double-check this locally by running in only once and seeing if
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll double check that. Edit: This is already defined at this point.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for checking! |
||
| ...options, | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| applySdkMetadata(sentryOptions, 'nuxt', ['nuxt', 'node']); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: Maybe we can also add some suggestions on what to do here, like "Rerun the the test" or something along that?