Skip to content

Commit fc6d51c

Browse files
authored
test(react): Ensure react router 3 tests always have correct types (#14949)
Noticed this failing every now and then locally. After looking into this, I found this to be a bit weird anyhow: 1. We used react-router-5 types for react-router 4, seems off 🤔 I adjusted this to v4 2. We used to add the `@types/react-router-v3` alias in package.json, but this was not being picked up anyhow properly. The reason is that this version of the types package basically re-exports a bunch of stuff from a path like `react-router/lib` which our resolution then sometimes (?) picks up from the v6 react-router package. Which is also why we had to overwrite this somehow, which is what sometimes failed (?). Now, we simply define these types in test/globals.d.ts properly, the same way as before, but there should be no more conflicts and we can safe installing one unecessary package.
1 parent 2327642 commit fc6d51c

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

packages/react/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@
5454
"@types/hoist-non-react-statics": "^3.3.5",
5555
"@types/node-fetch": "^2.6.11",
5656
"@types/react": "17.0.3",
57-
"@types/react-router-3": "npm:@types/[email protected]",
58-
"@types/react-router-4": "npm:@types/[email protected]",
59-
"@types/react-router-5": "npm:@types/[email protected]",
57+
"@types/react-router-4": "npm:@types/[email protected]",
58+
"@types/react-router-5": "npm:@types/[email protected]",
6059
"eslint-plugin-react": "^7.20.5",
6160
"eslint-plugin-react-hooks": "^4.0.8",
6261
"history-4": "npm:[email protected]",

packages/react/test/global.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Have to manually set types because we are using package-alias
2+
// Note that using e.g. ` "@types/react-router-3": "npm:@types/[email protected]",` in package.json does not work,
3+
// because the react-router v3 types re-export types from react-router/lib which ends up being the react router v6 types
4+
// So instead, we provide the types manually here
5+
declare module 'react-router-3' {
6+
import type * as React from 'react';
7+
import type { Match, Route as RouteType } from '../src/reactrouterv3';
8+
9+
type History = { replace: (s: string) => void; push: (s: string) => void };
10+
export function createMemoryHistory(): History;
11+
export const Router: React.ComponentType<{ history: History }>;
12+
export const Route: React.ComponentType<{ path: string; component?: React.ComponentType<any> }>;
13+
export const IndexRoute: React.ComponentType<{ component: React.ComponentType<any> }>;
14+
export const match: Match;
15+
export const createRoutes: (routes: any) => RouteType[];
16+
}

packages/react/test/reactrouterv3.test.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,11 @@ import {
88
setCurrentClient,
99
} from '@sentry/core';
1010
import { act, render } from '@testing-library/react';
11+
// biome-ignore lint/nursery/noUnusedImports: <explanation>
1112
import * as React from 'react';
1213
import { IndexRoute, Route, Router, createMemoryHistory, createRoutes, match } from 'react-router-3';
13-
14-
import type { Match, Route as RouteType } from '../src/reactrouterv3';
1514
import { reactRouterV3BrowserTracingIntegration } from '../src/reactrouterv3';
1615

17-
// Have to manually set types because we are using package-alias
18-
declare module 'react-router-3' {
19-
type History = { replace: (s: string) => void; push: (s: string) => void };
20-
export function createMemoryHistory(): History;
21-
export const Router: React.ComponentType<{ history: History }>;
22-
export const Route: React.ComponentType<{ path: string; component?: React.ComponentType<any> }>;
23-
export const IndexRoute: React.ComponentType<{ component: React.ComponentType<any> }>;
24-
export const match: Match;
25-
export const createRoutes: (routes: any) => RouteType[];
26-
}
27-
2816
const mockStartBrowserTracingPageLoadSpan = jest.fn();
2917
const mockStartBrowserTracingNavigationSpan = jest.fn();
3018

yarn.lock

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10128,10 +10128,10 @@
1012810128
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.8.tgz#49348387983075705fe8f4e02fb67f7daaec4934"
1012910129
integrity sha512-S78QIYirQcUoo6UJZx9CSP0O2ix9IaeAXwQi26Rhr/+mg7qqPy8TzaxHSUut7eGjL8WmLccT7/MXf304WjqHcA==
1013010130

10131-
"@types/history@^3":
10132-
version "3.2.4"
10133-
resolved "https://registry.yarnpkg.com/@types/history/-/history-3.2.4.tgz#0b6c62240d1fac020853aa5608758991d9f6ef3d"
10134-
integrity sha512-q7x8QeCRk2T6DR2UznwYW//mpN5uNlyajkewH2xd1s1ozCS4oHFRg2WMusxwLFlE57EkUYsd/gCapLBYzV3ffg==
10131+
"@types/history@^4.7.11":
10132+
version "4.7.11"
10133+
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.11.tgz#56588b17ae8f50c53983a524fc3cc47437969d64"
10134+
integrity sha512-qjDJRrmvBMiTx+jyLxvLfJU7UznFuokDv4f3WRuriHKERccVpFU+8XMQUAbDzoiJCsmexxRExQeMwwCdamSKDA==
1013510135

1013610136
"@types/hoist-non-react-statics@^3.3.5":
1013710137
version "3.3.5"
@@ -10427,28 +10427,20 @@
1042710427
dependencies:
1042810428
"@types/react" "*"
1042910429

10430-
"@types/react-router-3@npm:@types/[email protected]":
10431-
version "3.0.24"
10432-
resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-3.0.24.tgz#f924569538ea78a0b0d70892900a0d99ed6d7354"
10433-
integrity sha512-cSpMXzI0WocB5/UmySAtGlvG5w3m2mNvU6FgYFFWGqt6KywI7Ez+4Z9mEkglcAAGaP+voZjVg+BJP86bkVrSxQ==
10434-
dependencies:
10435-
"@types/history" "^3"
10436-
"@types/react" "*"
10437-
10438-
"@types/react-router-4@npm:@types/[email protected]":
10439-
version "5.1.14"
10440-
resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-5.1.14.tgz#e0442f4eb4c446541ad7435d44a97f8fe6df40da"
10441-
integrity sha512-LAJpqYUaCTMT2anZheoidiIymt8MuX286zoVFPM3DVb23aQBH0mAkFvzpd4LKqiolV8bBtZWT5Qp7hClCNDENw==
10430+
"@types/react-router-4@npm:@types/[email protected]":
10431+
version "4.0.25"
10432+
resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-4.0.25.tgz#1e25490780b80e0d8f96bf649379cca8638c1e5a"
10433+
integrity sha512-IsFvDwQy2X08g+tRMvugH1l7e0kkR+o5qEUkFfYLmjw2jGCPogY2bBuRAgZCZ5CSUswdNTkKtPUmNo+f6afyfg==
1044210434
dependencies:
1044310435
"@types/history" "*"
1044410436
"@types/react" "*"
1044510437

10446-
"@types/react-router-5@npm:@types/[email protected].14":
10447-
version "5.1.14"
10448-
resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-5.1.14.tgz#e0442f4eb4c446541ad7435d44a97f8fe6df40da"
10449-
integrity sha512-LAJpqYUaCTMT2anZheoidiIymt8MuX286zoVFPM3DVb23aQBH0mAkFvzpd4LKqiolV8bBtZWT5Qp7hClCNDENw==
10438+
"@types/react-router-5@npm:@types/[email protected].20":
10439+
version "5.1.20"
10440+
resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-5.1.20.tgz#88eccaa122a82405ef3efbcaaa5dcdd9f021387c"
10441+
integrity sha512-jGjmu/ZqS7FjSH6owMcD5qpq19+1RS9DeVRqfl1FeBMxTDQAGwlMWOcs52NDoXaNKyG3d1cYQFMs9rCrb88o9Q==
1045010442
dependencies:
10451-
"@types/history" "*"
10443+
"@types/history" "^4.7.11"
1045210444
"@types/react" "*"
1045310445

1045410446
"@types/react-test-renderer@>=16.9.0":

0 commit comments

Comments
 (0)