Skip to content

Commit 3045202

Browse files
chargomeclaude
andcommitted
test(astro): Tie server island spans to the page load under test
Match the island segment by its referer so a segment from another test cannot satisfy the assertions, and drop the test that checked Chromium's preload reuse rather than SDK behavior. Refs JSSDK-38 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cfb15f1 commit 3045202

1 file changed

Lines changed: 27 additions & 53 deletions

File tree

‎dev-packages/e2e-tests/test-applications/astro-7/tests/tracing.serverIslands.test.ts‎

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
import type { Page, Request } from '@playwright/test';
21
import { expect, test } from '@playwright/test';
32
import type { SerializedStreamedSpan } from '@sentry-internal/test-utils';
43
import { collectStreamedSpansUntilSegment, getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils';
54

65
const APP_NAME = 'astro-7';
76

8-
const SERVER_ISLAND_SEGMENT_NAME = (method: 'GET' | 'POST'): string => `${method} /_server-islands/[name]`;
9-
10-
function recordServerIslandRequests(page: Page): Request[] {
11-
const requests: Request[] = [];
12-
page.on('request', request => {
13-
if (request.url().includes('/_server-islands/')) {
14-
requests.push(request);
15-
}
16-
});
17-
return requests;
18-
}
19-
20-
function waitForServerIslandSegment(method: 'GET' | 'POST'): Promise<SerializedStreamedSpan> {
7+
// Each test loads a different page, and the island request's referer is that page, so the segment can't come
8+
// from another test.
9+
function waitForServerIslandSegment(method: 'GET' | 'POST', pagePath: string): Promise<SerializedStreamedSpan> {
2110
return waitForStreamedSpan(
2211
APP_NAME,
23-
span => getSpanOp(span) === 'http.server' && span.is_segment && span.name === SERVER_ISLAND_SEGMENT_NAME(method),
12+
span =>
13+
getSpanOp(span) === 'http.server' &&
14+
span.is_segment &&
15+
span.name === `${method} /_server-islands/[name]` &&
16+
String(span.attributes['http.request.header.referer']?.value?.[0]).endsWith(pagePath),
2417
);
2518
}
2619

2720
function findPageloadSegment(spans: SerializedStreamedSpan[], name: string): SerializedStreamedSpan {
28-
return spans.find(span => getSpanOp(span) === 'pageload' && span.is_segment && span.name === name)!;
21+
const pageloadSegment = spans.find(span => getSpanOp(span) === 'pageload' && span.is_segment && span.name === name);
22+
expect(pageloadSegment).toBeDefined();
23+
return pageloadSegment!;
24+
}
25+
26+
function findServerIslandFetchSpan(spans: SerializedStreamedSpan[]): SerializedStreamedSpan {
27+
const fetchSpan = spans.find(
28+
span =>
29+
getSpanOp(span) === 'http.client' && String(span.attributes['url.full']?.value).includes('/_server-islands/'),
30+
);
31+
expect(fetchSpan).toBeDefined();
32+
return fetchSpan!;
2933
}
3034

3135
test.describe('tracing in static routes with server islands', () => {
@@ -34,7 +38,7 @@ test.describe('tracing in static routes with server islands', () => {
3438
// whole trace is in hand once the segment has arrived.
3539
const clientSpansPromise = collectStreamedSpansUntilSegment(APP_NAME, '/server-island');
3640

37-
const serverIslandEndpointSpanPromise = waitForServerIslandSegment('GET');
41+
const serverIslandEndpointSpanPromise = waitForServerIslandSegment('GET', '/server-island');
3842

3943
await page.goto('/server-island');
4044

@@ -87,33 +91,9 @@ test.describe('tracing in static routes with server islands', () => {
8791
await page.waitForTimeout(1000); // wait another sec to ensure no server span is sent
8892
});
8993

90-
test('requests a GET server island once', async ({ page }) => {
91-
const serverIslandRequests = recordServerIslandRequests(page);
92-
const clientSpansPromise = collectStreamedSpansUntilSegment(APP_NAME, '/server-island');
93-
94-
await page.goto('/server-island');
95-
await page.getByAltText('User avatar').waitFor();
96-
const clientSpans = await clientSpansPromise;
97-
98-
expect(serverIslandRequests.map(request => request.method())).toEqual(['GET']);
99-
100-
const islandRequestHeaders = await serverIslandRequests[0]!.allHeaders();
101-
expect(islandRequestHeaders['sentry-trace']).toBeUndefined();
102-
expect(islandRequestHeaders['baggage']).toBeUndefined();
103-
104-
// The island script's `fetch()` is instrumented and gets trace headers, but the browser answers it with the
105-
// preload response, which left before the SDK ran. So the island render never sees the pageload's trace.
106-
const islandFetchSpan = clientSpans.find(
107-
span =>
108-
getSpanOp(span) === 'http.client' && String(span.attributes['url.full']?.value).includes('/_server-islands/'),
109-
);
110-
expect(islandFetchSpan).toBeDefined();
111-
});
112-
11394
test('connects a POST server island to the pageload trace', async ({ page }) => {
114-
const serverIslandRequests = recordServerIslandRequests(page);
11595
const clientSpansPromise = collectStreamedSpansUntilSegment(APP_NAME, '/server-island-post');
116-
const serverIslandSegmentPromise = waitForServerIslandSegment('POST');
96+
const serverIslandSegmentPromise = waitForServerIslandSegment('POST', '/server-island-post');
11797

11898
await page.goto('/server-island-post');
11999
await page.getByAltText('User avatar').waitFor();
@@ -122,14 +102,9 @@ test.describe('tracing in static routes with server islands', () => {
122102
const clientPageloadSpan = findPageloadSegment(clientSpans, '/server-island-post');
123103
const serverIslandSegment = await serverIslandSegmentPromise;
124104

125-
expect(serverIslandRequests.map(request => request.method())).toEqual(['POST']);
126105
expect(await page.locator('link[rel="preload"][href*="/_server-islands/"]').count()).toBe(0);
127106

128-
const islandFetchSpan = clientSpans.find(
129-
span =>
130-
getSpanOp(span) === 'http.client' && String(span.attributes['url.full']?.value).includes('/_server-islands/'),
131-
)!;
132-
expect(islandFetchSpan).toBeDefined();
107+
const islandFetchSpan = findServerIslandFetchSpan(clientSpans);
133108
expect(islandFetchSpan.trace_id).toBe(clientPageloadSpan.trace_id);
134109

135110
expect(serverIslandSegment.trace_id).toBe(clientPageloadSpan.trace_id);
@@ -139,13 +114,12 @@ test.describe('tracing in static routes with server islands', () => {
139114

140115
test.describe('tracing in SSR routes with server islands', () => {
141116
test('connects the page render, the pageload and the GET server island', async ({ page }) => {
142-
const serverIslandRequests = recordServerIslandRequests(page);
143117
const pageRenderSegmentPromise = waitForStreamedSpan(
144118
APP_NAME,
145119
span => getSpanOp(span) === 'http.server' && span.is_segment && span.name === 'GET /server-island-ssr',
146120
);
147121
const clientSpansPromise = collectStreamedSpansUntilSegment(APP_NAME, '/server-island-ssr');
148-
const serverIslandSegmentPromise = waitForServerIslandSegment('GET');
122+
const serverIslandSegmentPromise = waitForServerIslandSegment('GET', '/server-island-ssr');
149123

150124
await page.goto('/server-island-ssr');
151125
await page.getByAltText('User avatar').waitFor();
@@ -158,9 +132,9 @@ test.describe('tracing in SSR routes with server islands', () => {
158132
expect(clientPageloadSpan.trace_id).toBe(pageRenderSegment.trace_id);
159133
expect(clientPageloadSpan.parent_span_id).toBe(pageRenderSegment.span_id);
160134

161-
expect(serverIslandRequests.map(request => request.method())).toEqual(['GET']);
162-
163-
// The preload request leaves before the SDK runs, so the island starts its own trace.
135+
// The island script's `fetch()` gets trace headers, but the browser answers it with the preload response, which
136+
// left before the SDK ran. So the island starts its own trace.
137+
expect(findServerIslandFetchSpan(clientSpans).trace_id).toBe(clientPageloadSpan.trace_id);
164138
expect(serverIslandSegment.trace_id).not.toBe(clientPageloadSpan.trace_id);
165139
});
166140
});

0 commit comments

Comments
 (0)