Skip to content

Commit bf0a138

Browse files
authored
test(react): Update react-create-hash-router E2E test (#12262)
Instead of just testing to send to Sentry, this now tests the actual payloads being sent. This kind of builds on top of #12259, where we specifically test the event sending now.
1 parent 91f6776 commit bf0a138

File tree

9 files changed

+208
-482
lines changed

9 files changed

+208
-482
lines changed

dev-packages/e2e-tests/test-applications/react-create-hash-router/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
]
4747
},
4848
"devDependencies": {
49+
"@sentry-internal/event-proxy-server": "link:../../../event-proxy-server",
4950
"@playwright/test": "1.26.1",
5051
"serve": "14.0.1"
5152
},

dev-packages/e2e-tests/test-applications/react-create-hash-router/playwright.config.ts renamed to dev-packages/e2e-tests/test-applications/react-create-hash-router/playwright.config.mjs

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import type { PlaywrightTestConfig } from '@playwright/test';
21
import { devices } from '@playwright/test';
32

3+
const serverPort = 3030;
4+
const eventProxyPort = 3031;
5+
46
/**
57
* See https://playwright.dev/docs/test-configuration.
68
*/
7-
const config: PlaywrightTestConfig = {
9+
const config = {
810
testDir: './tests',
911
/* Maximum time one test can run for. */
1012
timeout: 150_000,
@@ -32,6 +34,9 @@ const config: PlaywrightTestConfig = {
3234

3335
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
3436
trace: 'on-first-retry',
37+
38+
/* Base URL to use in actions like `await page.goto('/')`. */
39+
baseURL: `http://localhost:${serverPort}`,
3540
},
3641

3742
/* Configure projects for major browsers */
@@ -58,13 +63,19 @@ const config: PlaywrightTestConfig = {
5863
],
5964

6065
/* Run your local dev server before starting the tests */
61-
webServer: {
62-
command: 'pnpm start',
63-
port: 3030,
64-
env: {
65-
PORT: '3030',
66+
webServer: [
67+
{
68+
command: 'node start-event-proxy.mjs',
69+
port: eventProxyPort,
6670
},
67-
},
71+
{
72+
command: 'pnpm start',
73+
port: serverPort,
74+
env: {
75+
PORT: '3030',
76+
},
77+
},
78+
],
6879
};
6980

7081
export default config;

dev-packages/e2e-tests/test-applications/react-create-hash-router/src/index.tsx

+2-21
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,15 @@ Sentry.init({
3232
tracesSampleRate: 1.0,
3333
release: 'e2e-test',
3434

35+
tunnel: 'http://localhost:3031',
36+
3537
// Always capture replays, so we can test this properly
3638
replaysSessionSampleRate: 1.0,
3739
replaysOnErrorSampleRate: 0.0,
3840

3941
debug: true,
4042
});
4143

42-
Object.defineProperty(window, 'sentryReplayId', {
43-
get() {
44-
return replay['_replay'].session.id;
45-
},
46-
});
47-
48-
Sentry.addEventProcessor(event => {
49-
if (
50-
event.type === 'transaction' &&
51-
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
52-
) {
53-
const eventId = event.event_id;
54-
if (eventId) {
55-
window.recordedTransactions = window.recordedTransactions || [];
56-
window.recordedTransactions.push(eventId);
57-
}
58-
}
59-
60-
return event;
61-
});
62-
6344
const sentryCreateHashRouter = Sentry.wrapCreateBrowserRouter(createHashRouter);
6445

6546
const router = sentryCreateHashRouter([

dev-packages/e2e-tests/test-applications/react-create-hash-router/src/pages/Index.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as Sentry from '@sentry/react';
21
// biome-ignore lint/nursery/noUnusedImports: Need React import for JSX
32
import * as React from 'react';
43
import { Link } from 'react-router-dom';
@@ -11,8 +10,7 @@ const Index = () => {
1110
value="Capture Exception"
1211
id="exception-button"
1312
onClick={() => {
14-
const eventId = Sentry.captureException(new Error('I am an error!'));
15-
window.capturedExceptionId = eventId;
13+
throw new Error('I am an error!');
1614
}}
1715
/>
1816
<Link to="/user/5" id="navigation">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { startEventProxyServer } from '@sentry-internal/event-proxy-server';
2+
3+
startEventProxyServer({
4+
port: 3031,
5+
proxyServerName: 'react-create-hash-router',
6+
});

dev-packages/e2e-tests/test-applications/react-create-hash-router/tests/behaviour-test.spec.ts

-204
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForError } from '@sentry-internal/event-proxy-server';
3+
4+
test('Captures exception correctly', async ({ page }) => {
5+
const errorEventPromise = waitForError('react-create-hash-router', event => {
6+
return !event.type && event.exception?.values?.[0]?.value === 'I am an error!';
7+
});
8+
9+
await page.goto('/');
10+
11+
const exceptionButton = page.locator('id=exception-button');
12+
await exceptionButton.click();
13+
14+
const errorEvent = await errorEventPromise;
15+
16+
expect(errorEvent.exception?.values).toHaveLength(1);
17+
expect(errorEvent.exception?.values?.[0]?.value).toBe('I am an error!');
18+
19+
expect(errorEvent.request).toEqual({
20+
headers: expect.any(Object),
21+
url: 'http://localhost:3030/',
22+
});
23+
24+
expect(errorEvent.transaction).toEqual('/');
25+
26+
expect(errorEvent.contexts?.trace).toEqual({
27+
trace_id: expect.any(String),
28+
span_id: expect.any(String),
29+
});
30+
});

0 commit comments

Comments
 (0)