Skip to content

Commit da758e3

Browse files
author
Luca Forstner
authored
test(e2e): Assert correct isolation scopes in Next.js (#11480)
1 parent 7383f8a commit da758e3

File tree

20 files changed

+251
-92
lines changed

20 files changed

+251
-92
lines changed

dev-packages/e2e-tests/test-applications/nextjs-14/app/generation-functions/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { getDefaultIsolationScope } from '@sentry/core';
2+
import * as Sentry from '@sentry/nextjs';
3+
14
export const dynamic = 'force-dynamic';
25

36
export default function Page() {
@@ -9,6 +12,9 @@ export async function generateMetadata({
912
}: {
1013
searchParams: { [key: string]: string | string[] | undefined };
1114
}) {
15+
Sentry.setTag('my-isolated-tag', true);
16+
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope
17+
1218
if (searchParams['shouldThrowInGenerateMetadata']) {
1319
throw new Error('generateMetadata Error');
1420
}

dev-packages/e2e-tests/test-applications/nextjs-14/tests/generation-functions.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ test('Should send a transaction and an error event for a faulty generateMetadata
3737

3838
await page.goto(`/generation-functions?metadataTitle=${testTitle}&shouldThrowInGenerateMetadata=1`);
3939

40-
expect(await transactionPromise).toBeDefined();
41-
expect(await errorEventPromise).toBeDefined();
40+
const errorEvent = await errorEventPromise;
41+
const transactionEvent = await transactionPromise;
42+
43+
// Assert that isolation scope works properly
44+
expect(errorEvent.tags?.['my-isolated-tag']).toBe(true);
45+
expect(errorEvent.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
46+
expect(transactionEvent.tags?.['my-isolated-tag']).toBe(true);
47+
expect(transactionEvent.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
4248
});
4349

4450
test('Should send a transaction event for a generateViewport() function invokation', async ({ page }) => {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { getDefaultIsolationScope } from '@sentry/core';
2+
import * as Sentry from '@sentry/nextjs';
3+
14
export const dynamic = 'force-dynamic';
25

36
export const runtime = 'edge';
47

58
export default async function Page() {
9+
Sentry.setTag('my-isolated-tag', true);
10+
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope
611
throw new Error('Edge Server Component Error');
712
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import { getDefaultIsolationScope } from '@sentry/core';
2+
import * as Sentry from '@sentry/nextjs';
3+
14
export const dynamic = 'force-dynamic';
25

36
export const runtime = 'edge';
47

58
export default async function Page() {
9+
Sentry.setTag('my-isolated-tag', true);
10+
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope
11+
612
return <h1>Hello world!</h1>;
713
}

dev-packages/e2e-tests/test-applications/nextjs-app-dir/app/route-handlers/[param]/edge/route.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getDefaultIsolationScope } from '@sentry/core';
2+
import * as Sentry from '@sentry/nextjs';
13
import { NextResponse } from 'next/server';
24

35
export const runtime = 'edge';
@@ -7,5 +9,8 @@ export async function PATCH() {
79
}
810

911
export async function DELETE(): Promise<Response> {
12+
Sentry.setTag('my-isolated-tag', true);
13+
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope
14+
1015
throw new Error('route-handler-edge-error');
1116
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import { getDefaultIsolationScope } from '@sentry/core';
2+
import * as Sentry from '@sentry/nextjs';
3+
14
export async function PUT(): Promise<Response> {
5+
Sentry.setTag('my-isolated-tag', true);
6+
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope
7+
28
throw new Error('route-handler-error');
39
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getDefaultIsolationScope } from '@sentry/core';
2+
import * as Sentry from '@sentry/nextjs';
3+
4+
export const dynamic = 'force-dynamic';
5+
6+
export default async function FaultyServerComponent() {
7+
Sentry.setTag('my-isolated-tag', true);
8+
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope
9+
10+
if (Math.random() + 1 > 0) {
11+
throw new Error('I am a faulty server component');
12+
}
13+
14+
return null;
15+
}

dev-packages/e2e-tests/test-applications/nextjs-app-dir/middleware.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { getDefaultIsolationScope } from '@sentry/core';
2+
import * as Sentry from '@sentry/nextjs';
13
import { NextResponse } from 'next/server';
24
import type { NextRequest } from 'next/server';
35

46
export async function middleware(request: NextRequest) {
7+
Sentry.setTag('my-isolated-tag', true);
8+
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope
9+
510
if (request.headers.has('x-should-throw')) {
611
throw new Error('Middleware Error');
712
}

dev-packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/edge-endpoint.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import { getDefaultIsolationScope } from '@sentry/core';
2+
import * as Sentry from '@sentry/nextjs';
3+
14
export const config = {
25
runtime: 'edge',
36
};
47

58
export default async function handler() {
9+
Sentry.setTag('my-isolated-tag', true);
10+
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope
11+
612
return new Response(
713
JSON.stringify({
814
name: 'Jim Halpert',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import { getDefaultIsolationScope } from '@sentry/core';
2+
import * as Sentry from '@sentry/nextjs';
3+
14
export const config = { runtime: 'edge' };
25

36
export default () => {
7+
Sentry.setTag('my-isolated-tag', true);
8+
Sentry.setTag('my-global-scope-isolated-tag', getDefaultIsolationScope().getScopeData().tags['my-isolated-tag']); // We set this tag to be able to assert that the previously set tag has not leaked into the global isolation scope
49
throw new Error('Edge Route Error');
510
};

0 commit comments

Comments
 (0)