Skip to content
17 changes: 17 additions & 0 deletions spec/ErrorBoundary.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ describe('<ErrorBoundary />', () => {
await waitFor(() => expect(mockPost).toHaveBeenCalledTimes(1));
});

it('should attach componentStack as a plain string (React Native friendly)', async () => {
render(
<ErrorBoundary scope={scope} fallback={BasicFallback}>
<BlowUp />
</ErrorBoundary>
);

await waitFor(() => expect(mockPost).toHaveBeenCalledTimes(1));

const [, options] = mockPost.mock.calls[0];
expect(options.attachments).toHaveLength(1);
const [attachment] = options.attachments;
expect(attachment.filename).toBe('componentStack.txt');
expect(typeof attachment.data).toBe('string');
expect(attachment.data).toContain('BlowUp');
});

it('should call beforePost', async () => {
render(
<ErrorBoundary
Expand Down
9 changes: 7 additions & 2 deletions src/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ function isArrayDiff(a: unknown[] = [], b: unknown[] = []) {
}

/**
* Pack a component stack trace string into an attachment
* Pack a component stack trace string into an attachment.
*
* The stack is appended as a plain string (not wrapped in a `Blob`) so that
* React Native's FormData polyfill — which can't serialize browser `Blob`
* objects — can upload it successfully. Browsers accept strings on
* `FormData.append()` as well, so this works everywhere.
*/
function createComponentStackAttachment(
componentStack: string
): BugSplatAttachment {
return {
filename: 'componentStack.txt',
data: new Blob([componentStack]),
data: componentStack,
};
}

Expand Down
Loading