Skip to content

Commit df34772

Browse files
committed
test(e2e): Add tests for react-router createHashRouter usage.
1 parent 50ead24 commit df34772

File tree

14 files changed

+829
-0
lines changed

14 files changed

+829
-0
lines changed
Lines changed: 29 additions & 0 deletions
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
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@sentry:registry=http://localhost:4873
2+
@sentry-internal:registry=http://localhost:4873
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "react-create-hash-router-test",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@sentry/react": "*",
7+
"@testing-library/jest-dom": "5.14.1",
8+
"@testing-library/react": "13.0.0",
9+
"@testing-library/user-event": "13.2.1",
10+
"@types/jest": "27.0.1",
11+
"@types/node": "16.7.13",
12+
"@types/react": "18.0.0",
13+
"@types/react-dom": "18.0.0",
14+
"react": "18.2.0",
15+
"react-dom": "18.2.0",
16+
"react-router-dom": "^6.4.1",
17+
"react-scripts": "5.0.1",
18+
"typescript": "4.4.2",
19+
"web-vitals": "2.1.0"
20+
},
21+
"scripts": {
22+
"build": "react-scripts build",
23+
"start": "serve -s build",
24+
"test": "playwright test"
25+
},
26+
"eslintConfig": {
27+
"extends": [
28+
"react-app",
29+
"react-app/jest"
30+
]
31+
},
32+
"browserslist": {
33+
"production": [
34+
">0.2%",
35+
"not dead",
36+
"not op_mini all"
37+
],
38+
"development": [
39+
"last 1 chrome version",
40+
"last 1 firefox version",
41+
"last 1 safari version"
42+
]
43+
},
44+
"devDependencies": {
45+
"@playwright/test": "1.26.1",
46+
"axios": "1.1.2",
47+
"serve": "14.0.1"
48+
},
49+
"volta": {
50+
"node": "16.19.0",
51+
"yarn": "1.22.19"
52+
}
53+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { PlaywrightTestConfig } from '@playwright/test';
2+
import { devices } from '@playwright/test';
3+
4+
/**
5+
* See https://playwright.dev/docs/test-configuration.
6+
*/
7+
const config: PlaywrightTestConfig = {
8+
testDir: './tests',
9+
/* Maximum time one test can run for. */
10+
timeout: 60 * 1000,
11+
expect: {
12+
/**
13+
* Maximum time expect() should wait for the condition to be met.
14+
* For example in `await expect(locator).toHaveText();`
15+
*/
16+
timeout: 5000,
17+
},
18+
/* Run tests in files in parallel */
19+
fullyParallel: true,
20+
/* Fail the build on CI if you accidentally left test.only in the source code. */
21+
forbidOnly: !!process.env.CI,
22+
/* Retry on CI only */
23+
retries: 0,
24+
/* Opt out of parallel tests on CI. */
25+
workers: 1,
26+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
27+
reporter: 'list',
28+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
29+
use: {
30+
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
31+
actionTimeout: 0,
32+
33+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
34+
trace: 'on-first-retry',
35+
},
36+
37+
/* Configure projects for major browsers */
38+
projects: [
39+
{
40+
name: 'chromium',
41+
use: {
42+
...devices['Desktop Chrome'],
43+
},
44+
},
45+
// For now we only test Chrome!
46+
// {
47+
// name: 'firefox',
48+
// use: {
49+
// ...devices['Desktop Firefox'],
50+
// },
51+
// },
52+
// {
53+
// name: 'webkit',
54+
// use: {
55+
// ...devices['Desktop Safari'],
56+
// },
57+
// },
58+
],
59+
60+
/* Run your local dev server before starting the tests */
61+
webServer: {
62+
command: 'pnpm start',
63+
port: Number(process.env.BASE_PORT) + Number(process.env.PORT_MODULO),
64+
env: {
65+
PORT: String(Number(process.env.BASE_PORT) + Number(process.env.PORT_MODULO)),
66+
},
67+
},
68+
};
69+
70+
export default config;
Lines changed: 24 additions & 0 deletions
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>
Lines changed: 5 additions & 0 deletions
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+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom/client';
3+
import * as Sentry from '@sentry/react';
4+
import {
5+
useLocation,
6+
useNavigationType,
7+
createRoutesFromChildren,
8+
matchRoutes,
9+
RouterProvider,
10+
createHashRouter,
11+
} from 'react-router-dom';
12+
import Index from './pages/Index';
13+
import User from './pages/User';
14+
15+
const replay = new Sentry.Replay();
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+
new Sentry.BrowserTracing({
22+
routingInstrumentation: Sentry.reactRouterV6Instrumentation(
23+
React.useEffect,
24+
useLocation,
25+
useNavigationType,
26+
createRoutesFromChildren,
27+
matchRoutes,
28+
),
29+
}),
30+
replay,
31+
],
32+
// We recommend adjusting this value in production, or using tracesSampler
33+
// for finer control
34+
tracesSampleRate: 1.0,
35+
release: 'e2e-test',
36+
37+
// Always capture replays, so we can test this properly
38+
replaysSessionSampleRate: 1.0,
39+
replaysOnErrorSampleRate: 0.0,
40+
});
41+
42+
Object.defineProperty(window, 'sentryReplayId', {
43+
get() {
44+
return replay['_replay'].session.id;
45+
},
46+
});
47+
48+
Sentry.addGlobalEventProcessor(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+
63+
const sentryCreateHashRouter = Sentry.wrapCreateBrowserRouter(createHashRouter);
64+
65+
const router = sentryCreateHashRouter([
66+
{
67+
path: '/',
68+
element: <Index />,
69+
},
70+
{
71+
path: '/user/:id',
72+
element: <User />,
73+
},
74+
]);
75+
76+
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
77+
78+
root.render(<RouterProvider router={router} />);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from 'react';
2+
import * as Sentry from '@sentry/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+
const eventId = Sentry.captureException(new Error('I am an error!'));
14+
window.capturedExceptionId = eventId;
15+
}}
16+
/>
17+
<Link to="/user/5" id="navigation">
18+
navigate
19+
</Link>
20+
</>
21+
);
22+
};
23+
24+
export default Index;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from 'react';
2+
3+
const User = () => {
4+
return <p>I am a blank page :)</p>;
5+
};
6+
7+
export default User;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="react-scripts" />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "../../test-recipe-schema.json",
3+
"testApplicationName": "react-create-hash-router",
4+
"buildCommand": "pnpm install && npx playwright install && pnpm build",
5+
"tests": [
6+
{
7+
"testName": "Playwright tests",
8+
"testCommand": "pnpm test"
9+
}
10+
],
11+
"canaryVersions": [
12+
{
13+
"dependencyOverrides": {
14+
"react": "latest",
15+
"react-dom": "latest"
16+
}
17+
}
18+
]
19+
}

0 commit comments

Comments
 (0)