Skip to content

Commit

Permalink
Update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyad-elabid-nw committed Jul 19, 2023
1 parent 2c37943 commit a0ce78d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/react/src/errorboundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Error, boolean>();

function recurse(error: Error & { cause?: Error }, cause: Error): void {
Expand Down
15 changes: 7 additions & 8 deletions packages/react/test/errorboundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand All @@ -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', () => {
Expand Down

0 comments on commit a0ce78d

Please sign in to comment.