From ae913ff178d472eb87f9b1df7ebf495af80767b5 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 8 Jul 2024 09:20:47 +0000 Subject: [PATCH 1/2] test(nextjs): Fix Next.js canary tests --- .../test-outgoing-fetch-external-disallowed/route.ts | 6 +++--- .../nextjs-14/app/propagation/test-outgoing-fetch/route.ts | 4 +++- .../nextjs-app-dir/tests/client-errors.test.ts | 7 +++++++ .../nextjs-app-dir/tests/transactions.test.ts | 5 +++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch-external-disallowed/route.ts b/dev-packages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch-external-disallowed/route.ts index b57d873f3ce7..be38866a9e94 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch-external-disallowed/route.ts +++ b/dev-packages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch-external-disallowed/route.ts @@ -3,8 +3,8 @@ import { NextResponse } from 'next/server'; export const dynamic = 'force-dynamic'; export async function GET() { - const data = await fetch(`http://localhost:3030/propagation/test-outgoing-fetch-external-disallowed/check`).then( - res => res.json(), - ); + const data = await fetch(`http://localhost:3030/propagation/test-outgoing-fetch-external-disallowed/check`, { + cache: 'no-store', + }).then(res => res.json()); return NextResponse.json(data); } diff --git a/dev-packages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch/route.ts b/dev-packages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch/route.ts index df9f2e772931..4ee7fac97f83 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch/route.ts +++ b/dev-packages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch/route.ts @@ -3,6 +3,8 @@ import { NextResponse } from 'next/server'; export const dynamic = 'force-dynamic'; export async function GET() { - const data = await fetch(`http://localhost:3030/propagation/test-outgoing-fetch/check`).then(res => res.json()); + const data = await fetch(`http://localhost:3030/propagation/test-outgoing-fetch/check`, { cache: 'no-store' }).then( + res => res.json(), + ); return NextResponse.json(data); } diff --git a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/client-errors.test.ts b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/client-errors.test.ts index b45f61e274a2..eb1e4e02a821 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/client-errors.test.ts +++ b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/client-errors.test.ts @@ -1,7 +1,12 @@ import { expect, test } from '@playwright/test'; import { waitForError } from '@sentry-internal/test-utils'; +const packageJson = require('../package.json'); + test('Sends a client-side exception to Sentry', async ({ page }) => { + const nextjsVersion = packageJson.dependencies.next; + const nextjsMajor = Number(nextjsVersion.split('.')[0]); + await page.goto('/'); const errorEventPromise = waitForError('nextjs-app-dir', errorEvent => { @@ -23,6 +28,8 @@ test('Sends a client-side exception to Sentry', async ({ page }) => { expect(errorEvent.transaction).toEqual('/'); expect(errorEvent.contexts?.trace).toEqual({ + // Next.js >= 15 propagates a trace ID to the client via a meta tag. + parent_span_id: nextjsMajor >= 15 ? expect.any(String) : undefined, trace_id: expect.any(String), span_id: expect.any(String), }); diff --git a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts index 94af99d6c386..52579091d325 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts +++ b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts @@ -4,6 +4,9 @@ import { waitForTransaction } from '@sentry-internal/test-utils'; const packageJson = require('../package.json'); test('Sends a pageload transaction', async ({ page }) => { + const nextjsVersion = packageJson.dependencies.next; + const nextjsMajor = Number(nextjsVersion.split('.')[0]); + const pageloadTransactionEventPromise = waitForTransaction('nextjs-app-dir', transactionEvent => { return transactionEvent?.contexts?.trace?.op === 'pageload' && transactionEvent?.transaction === '/'; }); @@ -23,6 +26,8 @@ test('Sends a pageload transaction', async ({ page }) => { version: expect.any(String), }, trace: { + // Next.js >= 15 propagates a trace ID to the client via a meta tag. + parent_span_id: nextjsMajor >= 15 ? expect.any(String) : undefined, span_id: expect.any(String), trace_id: expect.any(String), op: 'pageload', From 264b6eb510bda43d22f4b2a705edbf764239ac82 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 8 Jul 2024 11:53:50 +0000 Subject: [PATCH 2/2] check for dev mode --- .../nextjs-app-dir/tests/client-errors.test.ts | 6 ++++-- .../nextjs-app-dir/tests/transactions.test.ts | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/client-errors.test.ts b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/client-errors.test.ts index eb1e4e02a821..d1ea09ac2c76 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/client-errors.test.ts +++ b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/client-errors.test.ts @@ -6,6 +6,7 @@ const packageJson = require('../package.json'); test('Sends a client-side exception to Sentry', async ({ page }) => { const nextjsVersion = packageJson.dependencies.next; const nextjsMajor = Number(nextjsVersion.split('.')[0]); + const isDevMode = process.env.TEST_ENV === 'development'; await page.goto('/'); @@ -28,8 +29,9 @@ test('Sends a client-side exception to Sentry', async ({ page }) => { expect(errorEvent.transaction).toEqual('/'); expect(errorEvent.contexts?.trace).toEqual({ - // Next.js >= 15 propagates a trace ID to the client via a meta tag. - parent_span_id: nextjsMajor >= 15 ? expect.any(String) : undefined, + // Next.js >= 15 propagates a trace ID to the client via a meta tag. Also, only dev mode emits a meta tag because + // the requested page is static and only in dev mode SSR is kicked off. + parent_span_id: nextjsMajor >= 15 && isDevMode ? expect.any(String) : undefined, trace_id: expect.any(String), span_id: expect.any(String), }); diff --git a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts index 52579091d325..42fee84295b8 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts +++ b/dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts @@ -6,6 +6,7 @@ const packageJson = require('../package.json'); test('Sends a pageload transaction', async ({ page }) => { const nextjsVersion = packageJson.dependencies.next; const nextjsMajor = Number(nextjsVersion.split('.')[0]); + const isDevMode = process.env.TEST_ENV === 'development'; const pageloadTransactionEventPromise = waitForTransaction('nextjs-app-dir', transactionEvent => { return transactionEvent?.contexts?.trace?.op === 'pageload' && transactionEvent?.transaction === '/'; @@ -26,8 +27,9 @@ test('Sends a pageload transaction', async ({ page }) => { version: expect.any(String), }, trace: { - // Next.js >= 15 propagates a trace ID to the client via a meta tag. - parent_span_id: nextjsMajor >= 15 ? expect.any(String) : undefined, + // Next.js >= 15 propagates a trace ID to the client via a meta tag. Also, only dev mode emits a meta tag because + // the requested page is static and only in dev mode SSR is kicked off. + parent_span_id: nextjsMajor >= 15 && isDevMode ? expect.any(String) : undefined, span_id: expect.any(String), trace_id: expect.any(String), op: 'pageload',