Skip to content

Commit

Permalink
fix(react): use new captureException API
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Jun 5, 2020
1 parent 1d85628 commit 424ff83
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
5 changes: 1 addition & 4 deletions packages/react/src/errorboundary.tsx
Expand Up @@ -35,10 +35,7 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
public state: ErrorBoundaryState = INITIAL_STATE;

public componentDidCatch(error: Error, { componentStack }: React.ErrorInfo): void {
Sentry.withScope(scope => {
scope.setExtra('componentStack', componentStack);
Sentry.captureException(error);
});
Sentry.captureException(error, { contexts: { componentStack } });
const { onError, showDialog, dialogOptions } = this.props;
if (onError) {
onError(error, componentStack);
Expand Down
19 changes: 5 additions & 14 deletions packages/react/test/errorboundary.test.tsx
Expand Up @@ -3,22 +3,16 @@ import * as React from 'react';

import { ErrorBoundary, ErrorBoundaryProps, FALLBACK_ERR_MESSAGE } from '../src/errorboundary';

const mockSetExtra = jest.fn();
const mockCaptureException = jest.fn();
const mockShowReportDialog = jest.fn();

jest.mock('@sentry/browser', () => ({
captureException: (err: any) => {
mockCaptureException(err);
captureException: (err: any, ctx: any) => {
mockCaptureException(err, ctx);
},
showReportDialog: (options: any) => {
mockShowReportDialog(options);
},
withScope: (callback: Function) => {
callback({
setExtra: mockSetExtra,
});
},
}));

const TestApp: React.FC<ErrorBoundaryProps> = ({ children, ...props }) => {
Expand All @@ -45,7 +39,6 @@ describe('ErrorBoundary', () => {

afterEach(() => {
consoleErrorSpy.mockClear();
mockSetExtra.mockClear();
mockCaptureException.mockClear();
mockShowReportDialog.mockClear();
});
Expand Down Expand Up @@ -179,7 +172,6 @@ describe('ErrorBoundary', () => {

expect(mockOnError).toHaveBeenCalledTimes(0);
expect(mockCaptureException).toHaveBeenCalledTimes(0);
expect(mockSetExtra).toHaveBeenCalledTimes(0);

const btn = screen.getByTestId('errorBtn');
fireEvent.click(btn);
Expand All @@ -188,10 +180,9 @@ describe('ErrorBoundary', () => {
expect(mockOnError).toHaveBeenCalledWith(expect.any(Error), expect.any(String));

expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenCalledWith(expect.any(Error));

expect(mockSetExtra).toHaveBeenCalledTimes(1);
expect(mockSetExtra).toHaveBeenCalledWith('componentStack', expect.any(String));
expect(mockCaptureException).toHaveBeenCalledWith(expect.any(Error), {
contexts: { componentStack: expect.any(String) },
});
});

it('shows a Sentry Report Dialog with correct options', () => {
Expand Down

0 comments on commit 424ff83

Please sign in to comment.