From a0ce78da7de6b6587523a14f491fec34029662ae Mon Sep 17 00:00:00 2001 From: ziyad-elabid-nw Date: Wed, 19 Jul 2023 17:15:00 +0100 Subject: [PATCH] Update unit test --- packages/react/src/errorboundary.tsx | 2 +- packages/react/test/errorboundary.test.tsx | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/react/src/errorboundary.tsx b/packages/react/src/errorboundary.tsx index bc613c87172f..89f5a1795af2 100644 --- a/packages/react/src/errorboundary.tsx +++ b/packages/react/src/errorboundary.tsx @@ -67,7 +67,7 @@ const INITIAL_STATE = { eventId: null, }; -function setCause(error: Error & { cause?: Error }, cause: Error): void { +export function setCause(error: Error & { cause?: Error }, cause: Error): void { const seenErrors = new WeakMap(); function recurse(error: Error & { cause?: Error }, cause: Error): void { diff --git a/packages/react/test/errorboundary.test.tsx b/packages/react/test/errorboundary.test.tsx index ef97e07723f2..ee775a9e1015 100644 --- a/packages/react/test/errorboundary.test.tsx +++ b/packages/react/test/errorboundary.test.tsx @@ -4,7 +4,7 @@ import * as React from 'react'; import { useState } from 'react'; import type { ErrorBoundaryProps } from '../src/errorboundary'; -import { ErrorBoundary, isAtLeastReact17, UNKNOWN_COMPONENT, withErrorBoundary } from '../src/errorboundary'; +import { ErrorBoundary, isAtLeastReact17, setCause, UNKNOWN_COMPONENT, withErrorBoundary } from '../src/errorboundary'; const mockCaptureException = jest.fn(); const mockShowReportDialog = jest.fn(); @@ -382,15 +382,14 @@ describe('ErrorBoundary', () => { expect(cause.name).not.toContain('React ErrorBoundary'); }); - it('should truncate cause.message to 250 characters', () => { + it('should truncate cause.message to maxLengthValue = 250 ', () => { const mockOnError = jest.fn(); function CustomBam(): JSX.Element { - const error = new Error('bam'); - // The cause message with 610 characters + const error = new Error('Error example'); + // The cause message with 620 characters const cause = new Error('This is a very long cause message that exceeds 250 characters '.repeat(10)); - // @ts-ignore Need to set cause on error - error.cause = cause; + setCause(error, cause); throw error; } @@ -415,8 +414,8 @@ describe('ErrorBoundary', () => { const error = mockCaptureException.mock.calls[0][0]; const cause = error.cause; - // We need to make sure that the length of the cause message is 250 - expect(cause.message).toHaveLength(250); + // We need to make sure that the length of the cause message is 253 (3 characters of etc sign (...) in the truncate function) + expect(cause.message).toHaveLength(253); }); it('calls `beforeCapture()` when an error occurs', () => {