From d506e46938d009da4b72660e5a46c1bc3d9a3f6d Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 10 Dec 2021 22:49:09 -0500 Subject: [PATCH 1/2] ref(react): Stop using parseSemvar We don't need the bloated method from utils, just make our own --- packages/react/src/errorboundary.tsx | 9 ++++++--- packages/react/test/errorboundary.test.tsx | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/react/src/errorboundary.tsx b/packages/react/src/errorboundary.tsx index a5834e3340b9..0c73c7f9761d 100644 --- a/packages/react/src/errorboundary.tsx +++ b/packages/react/src/errorboundary.tsx @@ -1,9 +1,12 @@ import { captureException, ReportDialogOptions, Scope, showReportDialog, withScope } from '@sentry/browser'; -import { logger, parseSemver } from '@sentry/utils'; +import { logger } from '@sentry/utils'; import hoistNonReactStatics from 'hoist-non-react-statics'; import * as React from 'react'; -const reactVersion = parseSemver(React.version); +export function isAtLeastReact17(version: string): boolean { + const major = version.match(/^([^.]+)/); + return major !== null && parseInt(major[0]) >= 17; +} export const UNKNOWN_COMPONENT = 'unknown'; @@ -71,7 +74,7 @@ class ErrorBoundary extends React.Component= 17, create stack trace from componentStack param and links // to to the original error using `error.cause` otherwise relies on error param for stacktrace. // Linking errors requires the `LinkedErrors` integration be enabled. - if (reactVersion.major && reactVersion.major >= 17) { + if (isAtLeastReact17(React.version)) { const errorBoundaryError = new Error(error.message); errorBoundaryError.name = `React ErrorBoundary ${errorBoundaryError.name}`; errorBoundaryError.stack = componentStack; diff --git a/packages/react/test/errorboundary.test.tsx b/packages/react/test/errorboundary.test.tsx index f311b0d96154..1f9ae33232bc 100644 --- a/packages/react/test/errorboundary.test.tsx +++ b/packages/react/test/errorboundary.test.tsx @@ -3,7 +3,13 @@ import { fireEvent, render, screen } from '@testing-library/react'; import * as React from 'react'; import { useState } from 'react'; -import { ErrorBoundary, ErrorBoundaryProps, UNKNOWN_COMPONENT, withErrorBoundary } from '../src/errorboundary'; +import { + ErrorBoundary, + ErrorBoundaryProps, + UNKNOWN_COMPONENT, + withErrorBoundary, + isAtLeastReact17, +} from '../src/errorboundary'; const mockCaptureException = jest.fn(); const mockShowReportDialog = jest.fn(); @@ -326,3 +332,17 @@ describe('ErrorBoundary', () => { }); }); }); + +describe('isAtLeastReact17', () => { + test.each([ + ['React 15 with no patch', '15.0', false], + ['React 15 with no patch and no minor', '15.5', false], + ['React 16', '16.0.4', false], + ['React 17', '17.0.0', true], + ['React 17 with no patch', '17.4', true], + ['React 17 with no patch and no minor', '17', true], + ['React 18', '18.0.0', true], + ])('%s', (_: string, input: string, output: ReturnType) => { + expect(isAtLeastReact17(input)).toBe(output); + }); +}); From 98eb2d45393f408d38128737d293e80605521e79 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Sat, 11 Dec 2021 13:45:33 -0500 Subject: [PATCH 2/2] yarn fix --- packages/react/test/errorboundary.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react/test/errorboundary.test.tsx b/packages/react/test/errorboundary.test.tsx index 1f9ae33232bc..03297016ca50 100644 --- a/packages/react/test/errorboundary.test.tsx +++ b/packages/react/test/errorboundary.test.tsx @@ -6,9 +6,9 @@ import { useState } from 'react'; import { ErrorBoundary, ErrorBoundaryProps, + isAtLeastReact17, UNKNOWN_COMPONENT, withErrorBoundary, - isAtLeastReact17, } from '../src/errorboundary'; const mockCaptureException = jest.fn();