Skip to content

Commit c306b49

Browse files
committed
fix e2e tests
1 parent 532c03c commit c306b49

File tree

6 files changed

+35
-21
lines changed

6 files changed

+35
-21
lines changed

dev-packages/e2e-tests/test-applications/react-19/src/index.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import ReactDOM from 'react-dom/client';
55
import Index from './pages/Index';
66

77
Sentry.init({
8-
debug: true,
98
environment: 'qa', // dynamic sampling bias to keep transactions
109
dsn:
1110
process.env.REACT_APP_E2E_TEST_DSN ||
@@ -15,8 +14,12 @@ Sentry.init({
1514
});
1615

1716
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement, {
18-
onUncaughtError: Sentry.reactErrorHandler(),
19-
onCaughtError: Sentry.reactErrorHandler(),
17+
onUncaughtError: Sentry.reactErrorHandler((error, errorInfo) => {
18+
console.warn(error, errorInfo);
19+
}),
20+
onCaughtError: Sentry.reactErrorHandler((error, errorInfo) => {
21+
console.warn(error, errorInfo);
22+
}),
2023
});
2124

2225
root.render(

dev-packages/e2e-tests/test-applications/react-19/src/pages/Index.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class SampleErrorBoundary extends React.Component {
4242

4343
render() {
4444
if (this.state.error) {
45-
return <div>Caught an error: {this.state.error}</div>;
45+
return <div>Caught an error: {JSON.stringify(this.state.error)}</div>;
4646
}
4747
return this.props.children;
4848
}

dev-packages/e2e-tests/test-applications/react-19/tests/errors.test.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import { expect, test } from '@playwright/test';
22
import { waitForError } from '@sentry-internal/event-proxy-server';
33

44
test('Catches errors caught by error boundary', async ({ page }) => {
5+
page.on('console', message => {
6+
expect(message.text()).toContain('caught error');
7+
});
8+
59
const errorEventPromise = waitForError('react-19', event => {
6-
return !event.type && event.exception?.values?.[0]?.value === 'I am an error!';
10+
return !event.type && event.exception?.values?.[0]?.value === 'caught error';
711
});
812

913
await page.goto('/');
@@ -13,13 +17,17 @@ test('Catches errors caught by error boundary', async ({ page }) => {
1317

1418
const errorEvent = await errorEventPromise;
1519

16-
expect(errorEvent.exception?.values).toHaveLength(1);
20+
expect(errorEvent.exception?.values).toHaveLength(2);
1721
expect(errorEvent.exception?.values?.[0]?.value).toBe('caught error');
1822
});
1923

2024
test('Catches errors uncaught by error boundary', async ({ page }) => {
25+
page.on('console', message => {
26+
expect(message.text()).toContain('uncaught error');
27+
});
28+
2129
const errorEventPromise = waitForError('react-19', event => {
22-
return !event.type && event.exception?.values?.[0]?.value === 'I am an error!';
30+
return !event.type && event.exception?.values?.[0]?.value === 'uncaught error';
2331
});
2432

2533
await page.goto('/');
@@ -29,6 +37,6 @@ test('Catches errors uncaught by error boundary', async ({ page }) => {
2937

3038
const errorEvent = await errorEventPromise;
3139

32-
expect(errorEvent.exception?.values).toHaveLength(1);
40+
expect(errorEvent.exception?.values).toHaveLength(2);
3341
expect(errorEvent.exception?.values?.[0]?.value).toBe('uncaught error');
3442
});

packages/react/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@testing-library/react-hooks": "^7.0.2",
5757
"@types/history-4": "npm:@types/[email protected]",
5858
"@types/history-5": "npm:@types/[email protected]",
59-
"@types/hoist-non-react-statics": "^3.3.1",
59+
"@types/hoist-non-react-statics": "^3.3.5",
6060
"@types/node-fetch": "^2.6.0",
6161
"@types/react": "^18.0.0",
6262
"@types/react-router-3": "npm:@types/[email protected]",

packages/react/src/errorboundary.tsx

+11-8
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ export type ErrorBoundaryProps = {
3636
*/
3737
fallback?: React.ReactElement | FallbackRender | undefined;
3838
/** Called when the error boundary encounters an error */
39-
onError?: ((error: unknown, componentStack: string, eventId: string) => void) | undefined;
39+
onError?: ((error: unknown, componentStack: string | undefined, eventId: string) => void) | undefined;
4040
/** Called on componentDidMount() */
4141
onMount?: (() => void) | undefined;
4242
/** Called if resetError() is called from the fallback render props function */
43-
onReset?: ((error: unknown, componentStack: string | null, eventId: string | null) => void) | undefined;
43+
onReset?: ((error: unknown, componentStack: string | null | undefined, eventId: string | null) => void) | undefined;
4444
/** Called on componentWillUnmount() */
45-
onUnmount?: ((error: unknown, componentStack: string | null, eventId: string | null) => void) | undefined;
45+
onUnmount?: ((error: unknown, componentStack: string | null | undefined, eventId: string | null) => void) | undefined;
4646
/** Called before the error is captured by Sentry, allows for you to add tags or context using the scope */
4747
beforeCapture?: ((scope: Scope, error: unknown, componentStack: string | undefined) => void) | undefined;
4848
};
@@ -97,16 +97,19 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
9797

9898
public componentDidCatch(error: unknown, errorInfo: React.ErrorInfo): void {
9999
const { componentStack } = errorInfo;
100+
// TODO(v9): Remove this check and type `componentStack` to be React.ErrorInfo['componentStack'].
101+
const passedInComponentStack: string | undefined = componentStack == null ? undefined : componentStack;
102+
100103
const { beforeCapture, onError, showDialog, dialogOptions } = this.props;
101104
withScope(scope => {
102105
if (beforeCapture) {
103-
beforeCapture(scope, error, componentStack);
106+
beforeCapture(scope, error, passedInComponentStack);
104107
}
105108

106109
const eventId = captureReactException(error, errorInfo, { mechanism: { handled: !!this.props.fallback } });
107110

108111
if (onError) {
109-
onError(error, componentStack, eventId);
112+
onError(error, passedInComponentStack, eventId);
110113
}
111114
if (showDialog) {
112115
this._lastEventId = eventId;
@@ -186,7 +189,6 @@ function withErrorBoundary<P extends Record<string, any>>(
186189
WrappedComponent: React.ComponentType<P>,
187190
errorBoundaryOptions: ErrorBoundaryProps,
188191
): React.FC<P> {
189-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
190192
const componentDisplayName = WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
191193

192194
const Wrapped: React.FC<P> = (props: P) => (
@@ -195,12 +197,13 @@ function withErrorBoundary<P extends Record<string, any>>(
195197
</ErrorBoundary>
196198
);
197199

198-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
199200
Wrapped.displayName = `errorBoundary(${componentDisplayName})`;
200201

201202
// Copy over static methods from Wrapped component to Profiler HOC
202203
// See: https://reactjs.org/docs/higher-order-components.html#static-methods-must-be-copied-over
203-
hoistNonReactStatics(Wrapped, WrappedComponent);
204+
// Need to set type to any because of hoist-non-react-statics typing
205+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
206+
hoistNonReactStatics(Wrapped, WrappedComponent as any);
204207
return Wrapped;
205208
}
206209

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -8469,10 +8469,10 @@
84698469
resolved "https://registry.yarnpkg.com/@types/history/-/history-3.2.4.tgz#0b6c62240d1fac020853aa5608758991d9f6ef3d"
84708470
integrity sha512-q7x8QeCRk2T6DR2UznwYW//mpN5uNlyajkewH2xd1s1ozCS4oHFRg2WMusxwLFlE57EkUYsd/gCapLBYzV3ffg==
84718471

8472-
"@types/hoist-non-react-statics@^3.3.1":
8473-
version "3.3.1"
8474-
resolved "https://registry.yarnpkg.com/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz#1124aafe5118cb591977aeb1ceaaed1070eb039f"
8475-
integrity sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==
8472+
"@types/hoist-non-react-statics@^3.3.5":
8473+
version "3.3.5"
8474+
resolved "https://registry.yarnpkg.com/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.5.tgz#dab7867ef789d87e2b4b0003c9d65c49cc44a494"
8475+
integrity sha512-SbcrWzkKBw2cdwRTwQAswfpB9g9LJWfjtUeW/jvNwbhC8cpmmNYVePa+ncbUe0rGTQ7G3Ff6mYUN2VMfLVr+Sg==
84768476
dependencies:
84778477
"@types/react" "*"
84788478
hoist-non-react-statics "^3.3.0"

0 commit comments

Comments
 (0)