Skip to content

Commit 6f9bc8b

Browse files
authored
feat(v8/react): Add support for React Router createMemoryRouter (#14985)
1 parent 5144754 commit 6f9bc8b

33 files changed

+886
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
25+
/test-results/
26+
/playwright-report/
27+
/playwright/.cache/
28+
29+
!*.d.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@sentry:registry=http://127.0.0.1:4873
2+
@sentry-internal:registry=http://127.0.0.1:4873
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "react-create-browser-router-test",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@sentry/react": "latest || *",
7+
"@types/node": "^18.19.1",
8+
"@types/react": "18.0.0",
9+
"@types/react-dom": "18.0.0",
10+
"react": "18.2.0",
11+
"react-dom": "18.2.0",
12+
"react-router-dom": "^6.4.1",
13+
"react-scripts": "5.0.1",
14+
"typescript": "~5.0.0"
15+
},
16+
"scripts": {
17+
"build": "react-scripts build",
18+
"start": "serve -s build",
19+
"test": "playwright test",
20+
"clean": "npx rimraf node_modules pnpm-lock.yaml",
21+
"test:build": "pnpm install && pnpm build",
22+
"test:build-canary": "pnpm install && pnpm add react@canary react-dom@canary && pnpm build",
23+
"test:assert": "pnpm test"
24+
},
25+
"eslintConfig": {
26+
"extends": ["react-app", "react-app/jest"]
27+
},
28+
"browserslist": {
29+
"production": [">0.2%", "not dead", "not op_mini all"],
30+
"development": ["last 1 chrome version", "last 1 firefox version", "last 1 safari version"]
31+
},
32+
"devDependencies": {
33+
"@playwright/test": "^1.44.1",
34+
"@sentry-internal/test-utils": "link:../../../test-utils",
35+
"serve": "14.0.1"
36+
},
37+
"volta": {
38+
"extends": "../../package.json"
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getPlaywrightConfig } from '@sentry-internal/test-utils';
2+
3+
const config = getPlaywrightConfig({
4+
startCommand: `pnpm start`,
5+
});
6+
7+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<meta name="theme-color" content="#000000" />
7+
<meta name="description" content="Web site created using create-react-app" />
8+
<title>React App</title>
9+
</head>
10+
<body>
11+
<noscript>You need to enable JavaScript to run this app.</noscript>
12+
<div id="root"></div>
13+
<!--
14+
This HTML file is a template.
15+
If you open it directly in the browser, you will see an empty page.
16+
17+
You can add webfonts, meta tags, or analytics to this file.
18+
The build step will place the bundled scripts into the <body> tag.
19+
20+
To begin the development, run `npm start` or `yarn start`.
21+
To create a production bundle, use `npm run build` or `yarn build`.
22+
-->
23+
</body>
24+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface Window {
2+
recordedTransactions?: string[];
3+
capturedExceptionId?: string;
4+
sentryReplayId?: string;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as Sentry from '@sentry/react';
2+
import React from 'react';
3+
import ReactDOM from 'react-dom/client';
4+
import {
5+
RouterProvider,
6+
createBrowserRouter,
7+
createRoutesFromChildren,
8+
matchRoutes,
9+
useLocation,
10+
useNavigationType,
11+
} from 'react-router-dom';
12+
import Index from './pages/Index';
13+
import User from './pages/User';
14+
15+
const replay = Sentry.replayIntegration();
16+
17+
Sentry.init({
18+
// environment: 'qa', // dynamic sampling bias to keep transactions
19+
dsn: process.env.REACT_APP_E2E_TEST_DSN,
20+
integrations: [
21+
Sentry.reactRouterV6BrowserTracingIntegration({
22+
useEffect: React.useEffect,
23+
useLocation,
24+
useNavigationType,
25+
createRoutesFromChildren,
26+
matchRoutes,
27+
}),
28+
replay,
29+
],
30+
// We recommend adjusting this value in production, or using tracesSampler
31+
// for finer control
32+
tracesSampleRate: 1.0,
33+
release: 'e2e-test',
34+
35+
tunnel: 'http://localhost:3031',
36+
37+
// Always capture replays, so we can test this properly
38+
replaysSessionSampleRate: 1.0,
39+
replaysOnErrorSampleRate: 0.0,
40+
41+
debug: !!process.env.DEBUG,
42+
});
43+
44+
const sentryCreateBrowserRouter = Sentry.wrapCreateBrowserRouterV6(createBrowserRouter);
45+
46+
const router = sentryCreateBrowserRouter(
47+
[
48+
{
49+
path: '/',
50+
element: <Index />,
51+
},
52+
{
53+
path: '/user/:id',
54+
element: <User />,
55+
},
56+
],
57+
{
58+
// We're testing whether this option is avoided in the integration
59+
// We expect this to be ignored
60+
initialEntries: ['/user/1'],
61+
},
62+
);
63+
64+
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
65+
66+
root.render(<RouterProvider router={router} />);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// biome-ignore lint/nursery/noUnusedImports: Need React import for JSX
2+
import * as React from 'react';
3+
import { Link } from 'react-router-dom';
4+
5+
const Index = () => {
6+
return (
7+
<>
8+
<input
9+
type="button"
10+
value="Capture Exception"
11+
id="exception-button"
12+
onClick={() => {
13+
throw new Error('I am an error!');
14+
}}
15+
/>
16+
<Link to="/user/5" id="navigation">
17+
navigate
18+
</Link>
19+
</>
20+
);
21+
};
22+
23+
export default Index;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// biome-ignore lint/nursery/noUnusedImports: Need React import for JSX
2+
import * as React from 'react';
3+
4+
const User = () => {
5+
return <p>I am a blank page :)</p>;
6+
};
7+
8+
export default User;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="react-scripts" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { startEventProxyServer } from '@sentry-internal/test-utils';
2+
3+
startEventProxyServer({
4+
port: 3031,
5+
proxyServerName: 'react-create-browser-router',
6+
});
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/test-utils';
3+
4+
test('Captures exception correctly', async ({ page }) => {
5+
const errorEventPromise = waitForError('react-create-browser-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.stringMatching(/[a-f0-9]{32}/),
28+
span_id: expect.stringMatching(/[a-f0-9]{16}/),
29+
});
30+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForTransaction } from '@sentry-internal/test-utils';
3+
4+
test('Captures a pageload transaction', async ({ page }) => {
5+
const transactionEventPromise = waitForTransaction('react-create-browser-router', event => {
6+
return event.contexts?.trace?.op === 'pageload';
7+
});
8+
9+
await page.goto('/');
10+
11+
const transactionEvent = await transactionEventPromise;
12+
13+
expect(transactionEvent).toEqual(
14+
expect.objectContaining({
15+
transaction: '/',
16+
type: 'transaction',
17+
transaction_info: {
18+
source: 'route',
19+
},
20+
}),
21+
);
22+
23+
expect(transactionEvent.contexts?.trace).toEqual(
24+
expect.objectContaining({
25+
data: expect.objectContaining({
26+
deviceMemory: expect.any(String),
27+
effectiveConnectionType: expect.any(String),
28+
hardwareConcurrency: expect.any(String),
29+
'sentry.idle_span_finish_reason': 'idleTimeout',
30+
'sentry.op': 'pageload',
31+
'sentry.origin': 'auto.pageload.react.reactrouter_v6',
32+
'sentry.sample_rate': 1,
33+
'sentry.source': 'route',
34+
}),
35+
op: 'pageload',
36+
span_id: expect.stringMatching(/[a-f0-9]{16}/),
37+
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
38+
origin: 'auto.pageload.react.reactrouter_v6',
39+
}),
40+
);
41+
});
42+
43+
test('Captures a navigation transaction', async ({ page }) => {
44+
const transactionEventPromise = waitForTransaction('react-create-browser-router', event => {
45+
return event.contexts?.trace?.op === 'navigation';
46+
});
47+
48+
await page.goto('/');
49+
const linkElement = page.locator('id=navigation');
50+
await linkElement.click();
51+
52+
const transactionEvent = await transactionEventPromise;
53+
expect(transactionEvent.contexts?.trace).toEqual({
54+
data: expect.objectContaining({
55+
'sentry.idle_span_finish_reason': 'idleTimeout',
56+
'sentry.op': 'navigation',
57+
'sentry.origin': 'auto.navigation.react.reactrouter_v6',
58+
'sentry.sample_rate': 1,
59+
'sentry.source': 'route',
60+
}),
61+
op: 'navigation',
62+
span_id: expect.stringMatching(/[a-f0-9]{16}/),
63+
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
64+
origin: 'auto.navigation.react.reactrouter_v6',
65+
});
66+
67+
expect(transactionEvent).toEqual(
68+
expect.objectContaining({
69+
transaction: '/user/:id',
70+
type: 'transaction',
71+
transaction_info: {
72+
source: 'route',
73+
},
74+
}),
75+
);
76+
77+
expect(transactionEvent.spans).toEqual([]);
78+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2018",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"esModuleInterop": true,
8+
"allowSyntheticDefaultImports": true,
9+
"strict": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"noFallthroughCasesInSwitch": true,
12+
"module": "esnext",
13+
"moduleResolution": "node",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"noEmit": true,
17+
"jsx": "react"
18+
},
19+
"include": ["src", "tests"]
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
25+
/test-results/
26+
/playwright-report/
27+
/playwright/.cache/
28+
29+
!*.d.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@sentry:registry=http://127.0.0.1:4873
2+
@sentry-internal:registry=http://127.0.0.1:4873

0 commit comments

Comments
 (0)