Skip to content

Commit 3be9670

Browse files
chargomeclaude
andcommitted
test(e2e): Bring streaming cacheComponents app to parity with the static one
`nextjs-16-streaming-cacheComponents` covered only 5 of the 9 tests its static counterpart runs. It was missing the two on-demand Server Component capture tests, the `generateMetadata` prerender test and the whole hanging promise rejection regression spec, along with the routes those need. Ports the missing tests over and adds the webpack variant. The hanging promise regression is only observable under webpack, because server components are wrapped by `wrappingLoader`, which Turbopack builds do not run, so without that variant the ported spec would never exercise it. Ref #23802 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 3b05f75 commit 3be9670

10 files changed

Lines changed: 167 additions & 2 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as Sentry from '@sentry/nextjs';
2+
3+
// Only `test` is prerendered at build time. `exception` and `message` are generated
4+
// on-demand at request time.
5+
export function generateStaticParams() {
6+
return [{ id: 'test' }];
7+
}
8+
9+
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
10+
const { id } = await params;
11+
12+
if (id === 'exception') {
13+
Sentry.captureException(new Error('Test error from cache components page'));
14+
return <p id="result">Error captured for id exception</p>;
15+
}
16+
17+
if (id === 'message') {
18+
Sentry.captureMessage('Test message from cache components page');
19+
return <p id="result">Message captured for id message</p>;
20+
}
21+
22+
return <p id="result">Hello, {id}!</p>;
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function GET() {
2+
return Response.json({ value: 'hanging-fetch-data' });
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { captureException } from '@sentry/nextjs';
2+
import type { Metadata } from 'next';
3+
4+
/**
5+
* Calling captureException synchronously inside generateMetadata
6+
* during `next build` prerender (cacheComponents). uuid4 -> crypto.randomUUID() runs
7+
*/
8+
export const generateMetadata = (): Metadata => {
9+
captureException(new Error('diagnostic: data missing for this page'));
10+
return { title: 'capture-metadata' };
11+
};
12+
13+
export default function Page() {
14+
return <h1>capture-metadata</h1>;
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Loading() {
2+
return <div id="sentinel-loading">Loading...</div>;
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as Sentry from '@sentry/nextjs';
2+
3+
// Captures an error tagged with a caller-supplied unique token. Tests use this as a drain marker: the
4+
// token guarantees a cache miss, so the capture always happens on request, and its arrival proves the
5+
// event pipeline has drained past every earlier request.
6+
export default async function Page({ params }: { params: Promise<{ token: string }> }) {
7+
const { token } = await params;
8+
9+
Sentry.captureException(new Error(`error-sentinel-${token}`));
10+
11+
return <p id="sentinel">{token}</p>;
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Loading() {
2+
return <div id="loading">Loading...</div>;
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This `fetch()` deliberately has no cache configuration. Under Cache Components, Next.js does not
2+
// issue such a request during a prerender - it hands out a promise that never settles and rejects it
3+
// with a `HANGING_PROMISE_REJECTION` digest once the prerender is aborted. That rejection surfaces in
4+
// this component and therefore in the Sentry server component wrapper, which must not report it.
5+
export default async function Page() {
6+
const response = await fetch('http://localhost:3030/api/hanging-fetch-data');
7+
const data = (await response.json()) as { value: string };
8+
9+
return <p id="fetched-value">{data.value}</p>;
10+
}

dev-packages/e2e-tests/test-applications/nextjs-16-streaming-cacheComponents/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
"extends": "../../package.json"
4747
},
4848
"sentryTest": {
49-
"//": "TODO: Add variants for webpack once supported"
49+
"variants": [
50+
{
51+
"build-command": "pnpm test:build-webpack",
52+
"label": "nextjs-16-streaming-cacheComponents (webpack)",
53+
"assert-command": "pnpm test:assert-webpack"
54+
}
55+
]
5056
}
5157
}

dev-packages/e2e-tests/test-applications/nextjs-16-streaming-cacheComponents/tests/cacheComponents.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from '@playwright/test';
2-
import { waitForStreamedSpan, waitForStreamedSpans, getSpanOp } from '@sentry-internal/test-utils';
2+
import { getSpanOp, waitForError, waitForStreamedSpan, waitForStreamedSpans } from '@sentry-internal/test-utils';
33

44
test('Should render cached component', async ({ page }) => {
55
const spansPromise = waitForStreamedSpans('nextjs-16-streaming-cacheComponents', spans => {
@@ -49,6 +49,34 @@ test('Should generate metadata', async ({ page }) => {
4949
await expect(page).toHaveTitle('Cache Components Metadata Test');
5050
});
5151

52+
// Capturing an event inside a Server Component that is (re)generated at request time must not
53+
// trip Next.js Cache Components prerender guards (`new Date()` / `crypto`).
54+
test('Should capture an exception from an on-demand generated Server Component', async ({ page }) => {
55+
const errorPromise = waitForError('nextjs-16-streaming-cacheComponents', errorEvent => {
56+
return errorEvent.exception?.values?.[0]?.value === 'Test error from cache components page';
57+
});
58+
59+
await page.goto('/exception');
60+
61+
await expect(page.locator('#result')).toHaveText('Error captured for id exception');
62+
63+
const error = await errorPromise;
64+
expect(error.exception?.values?.[0]?.value).toBe('Test error from cache components page');
65+
});
66+
67+
test('Should capture a message from an on-demand generated Server Component', async ({ page }) => {
68+
const messagePromise = waitForError('nextjs-16-streaming-cacheComponents', errorEvent => {
69+
return errorEvent.message === 'Test message from cache components page';
70+
});
71+
72+
await page.goto('/message');
73+
74+
await expect(page.locator('#result')).toHaveText('Message captured for id message');
75+
76+
const message = await messagePromise;
77+
expect(message.message).toBe('Test message from cache components page');
78+
});
79+
5280
test('Should generate metadata async', async ({ page }) => {
5381
const spansPromise = waitForStreamedSpans('nextjs-16-streaming-cacheComponents', spans => {
5482
return spans.some(
@@ -96,3 +124,10 @@ test('Prerendered shell does not stitch the pageload onto a stale trace', async
96124
expect(await page.locator('meta[name="sentry-trace"]').count()).toBe(0);
97125
expect(await page.locator('meta[name="baggage"]').count()).toBe(0);
98126
});
127+
128+
test('Should prerender a page that captures an exception in generateMetadata', async ({ page }) => {
129+
await page.goto('/capture-metadata');
130+
131+
await expect(page).toHaveTitle('capture-metadata');
132+
await expect(page.locator('h1')).toHaveText('capture-metadata');
133+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { expect, test } from '@playwright/test';
2+
import { getSpanOp, waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils';
3+
4+
const HANGING_PROMISE_DIGEST_MESSAGE = 'rejects when the prerender is complete';
5+
6+
// Under Cache Components, Next.js aborts prerenders by rejecting the promises it handed out for
7+
// uncached `fetch()` calls. React discards those rejections - they never affect the response - so the
8+
// Sentry wrappers must not report them. See https://github.com/getsentry/sentry-javascript/issues/23592
9+
//
10+
// Note this only exercises the regression under the webpack variant: server components are wrapped by
11+
// `wrappingLoader`, which Turbopack builds do not run, so there is no wrapper to observe the rejection
12+
// there. Under Turbopack the test still asserts the route renders and reports no errors.
13+
test('does not capture hanging prerender promise rejections on a runtime prefetch', async ({ page, request }) => {
14+
const capturedHangingPromiseErrors: string[] = [];
15+
void waitForError('nextjs-16-streaming-cacheComponents', errorEvent => {
16+
const value = errorEvent.exception?.values?.[0]?.value ?? '';
17+
if (value.includes(HANGING_PROMISE_DIGEST_MESSAGE)) {
18+
capturedHangingPromiseErrors.push(value);
19+
}
20+
return false;
21+
});
22+
23+
// `Next-Router-Prefetch: 2` is what the Next.js router sends for a runtime prefetch. It makes Next.js
24+
// run a prerender at request time, which is what produces the hanging promise rejection. A plain
25+
// document request only replays the shell that was prerendered at build time and would not trigger it.
26+
const prefetchResponse = await request.get('/hanging-fetch', {
27+
headers: { RSC: '1', 'Next-Router-Prefetch': '2' },
28+
});
29+
expect(prefetchResponse.ok()).toBe(true);
30+
31+
const serverSpanPromise = waitForStreamedSpan('nextjs-16-streaming-cacheComponents', span => {
32+
return span.name === 'GET /hanging-fetch' && getSpanOp(span) === 'http.server' && span.is_segment;
33+
});
34+
35+
await page.goto('/hanging-fetch');
36+
await expect(page.locator('#fetched-value')).toHaveText('hanging-fetch-data');
37+
38+
expect(await serverSpanPromise).toBeDefined();
39+
40+
// Drain marker instead of a sleep: request a route that deliberately captures an error, tagged with a
41+
// token unique to this run so it can never be served from cache. It is requested strictly after the
42+
// prefetch, and the SDK flushes per request, so once this error arrives any error the prefetch had
43+
// captured must already have arrived too. That makes the assertion below "nothing was captured"
44+
// rather than "nothing had been captured yet". It doubles as a check that errors do flow at all.
45+
const token = `${Date.now()}`;
46+
const sentinelPromise = waitForError('nextjs-16-streaming-cacheComponents', errorEvent => {
47+
return errorEvent.exception?.values?.[0]?.value === `error-sentinel-${token}`;
48+
});
49+
50+
await page.goto(`/error-sentinel/${token}`);
51+
await expect(page.locator('#sentinel')).toHaveText(token);
52+
await sentinelPromise;
53+
54+
expect(capturedHangingPromiseErrors).toEqual([]);
55+
});

0 commit comments

Comments
 (0)