Skip to content

Commit 48bf789

Browse files
committed
add test for not recapturing
1 parent d42a724 commit 48bf789

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

packages/core/test/lib/base.test.ts

+65-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Hub, Scope, Session } from '@sentry/hub';
22
import { Event, Outcome, Severity, Span, Transport } from '@sentry/types';
33
import { logger, SentryError, SyncPromise } from '@sentry/utils';
44

5+
import { BaseClient } from '../../src/baseclient';
56
import * as integrationModule from '../../src/integration';
67
import { TestBackend } from '../mocks/backend';
78
import { TestClient } from '../mocks/client';
@@ -12,6 +13,9 @@ const PUBLIC_DSN = 'https://username@domain/123';
1213
// eslint-disable-next-line no-var
1314
declare var global: any;
1415

16+
const backendEventFromException = jest.spyOn(TestBackend.prototype, 'eventFromException');
17+
const clientProcess = jest.spyOn(TestClient.prototype as any, '_process');
18+
1519
jest.mock('@sentry/utils', () => {
1620
const original = jest.requireActual('@sentry/utils');
1721
return {
@@ -57,7 +61,7 @@ describe('BaseClient', () => {
5761
});
5862

5963
afterEach(() => {
60-
jest.restoreAllMocks();
64+
jest.clearAllMocks();
6165
});
6266

6367
describe('constructor() / getDsn()', () => {
@@ -249,6 +253,43 @@ describe('BaseClient', () => {
249253
}),
250254
);
251255
});
256+
257+
test.each([
258+
['`Error` instance', new Error('Will I get caught twice?')],
259+
['plain object', { 'Will I': 'get caught twice?' }],
260+
['primitive wrapper', new String('Will I get caught twice?')],
261+
])("doesn't capture the same exception twice - %s", (_name: string, thrown: any) => {
262+
const client = new TestClient({ dsn: PUBLIC_DSN });
263+
264+
expect(thrown.__sentry_captured__).toBeUndefined();
265+
266+
client.captureException(thrown);
267+
268+
expect(thrown.__sentry_captured__).toBe(true);
269+
expect(backendEventFromException).toHaveBeenCalledTimes(1);
270+
271+
client.captureException(thrown);
272+
273+
// `captureException` should bail right away this second time around and not get as far as calling this again
274+
expect(backendEventFromException).toHaveBeenCalledTimes(1);
275+
});
276+
277+
test("doesn't capture the same exception twice", () => {
278+
const client = new TestClient({ dsn: PUBLIC_DSN });
279+
const err = new Error('Will I get caught twice?') as any;
280+
281+
expect(err.__sentry_captured__).toBeUndefined();
282+
283+
client.captureException(err);
284+
285+
expect(err.__sentry_captured__).toBe(true);
286+
expect(backendEventFromException).toHaveBeenCalledTimes(1);
287+
288+
client.captureException(err);
289+
290+
// `captureException` should bail right away this second time around and not get as far as calling this again
291+
expect(backendEventFromException).toHaveBeenCalledTimes(1);
292+
});
252293
});
253294

254295
describe('captureMessage', () => {
@@ -325,6 +366,28 @@ describe('BaseClient', () => {
325366
expect(TestBackend.instance!.event).toBeUndefined();
326367
});
327368

369+
test.each([
370+
['`Error` instance', new Error('Will I get caught twice?')],
371+
['plain object', { 'Will I': 'get caught twice?' }],
372+
['primitive wrapper', new String('Will I get caught twice?')],
373+
])("doesn't capture the same exception twice - %s", (_name: string, thrown: any) => {
374+
const client = new TestClient({ dsn: PUBLIC_DSN });
375+
const event = { exception: { values: [{ type: 'Error', message: 'Will I get caught twice?' }] } };
376+
const hint = { originalException: thrown };
377+
378+
expect(thrown.__sentry_captured__).toBeUndefined();
379+
380+
client.captureEvent(event, hint);
381+
382+
expect(thrown.__sentry_captured__).toBe(true);
383+
expect(clientProcess).toHaveBeenCalledTimes(1);
384+
385+
client.captureEvent(event, hint);
386+
387+
// `captureEvent` should bail right away this second time around and not get as far as calling this again
388+
expect(clientProcess).toHaveBeenCalledTimes(1);
389+
});
390+
328391
test('sends an event', () => {
329392
expect.assertions(2);
330393
const client = new TestClient({ dsn: PUBLIC_DSN });
@@ -798,7 +861,7 @@ describe('BaseClient', () => {
798861
expect(TestBackend.instance!.event).toBeUndefined();
799862
});
800863

801-
test('calls beforeSend gets an access to a hint as a second argument', () => {
864+
test('beforeSend gets access to a hint as a second argument', () => {
802865
expect.assertions(2);
803866
const beforeSend = jest.fn((event, hint) => ({ ...event, data: hint.data }));
804867
const client = new TestClient({ dsn: PUBLIC_DSN, beforeSend });

0 commit comments

Comments
 (0)