|
1 | 1 | import { Hub, Scope } from '@sentry/hub';
|
2 | 2 | import { Event, Severity, Span } from '@sentry/types';
|
3 |
| -import { SentryError } from '@sentry/utils'; |
| 3 | +import { logger, SentryError } from '@sentry/utils'; |
4 | 4 |
|
5 | 5 | import { TestBackend } from '../mocks/backend';
|
6 | 6 | import { TestClient } from '../mocks/client';
|
@@ -55,6 +55,10 @@ describe('BaseClient', () => {
|
55 | 55 | TestBackend.instance = undefined;
|
56 | 56 | });
|
57 | 57 |
|
| 58 | + afterEach(() => { |
| 59 | + jest.restoreAllMocks(); |
| 60 | + }); |
| 61 | + |
58 | 62 | describe('constructor() / getDsn()', () => {
|
59 | 63 | test('returns the Dsn', () => {
|
60 | 64 | expect.assertions(1);
|
@@ -639,11 +643,30 @@ describe('BaseClient', () => {
|
639 | 643 | });
|
640 | 644 |
|
641 | 645 | test('calls beforeSend and discards the event', () => {
|
642 |
| - expect.assertions(1); |
| 646 | + expect.assertions(3); |
643 | 647 | const beforeSend = jest.fn(() => null);
|
644 | 648 | const client = new TestClient({ dsn: PUBLIC_DSN, beforeSend });
|
| 649 | + const captureExceptionSpy = jest.spyOn(client, 'captureException'); |
| 650 | + const loggerErrorSpy = jest.spyOn(logger, 'error'); |
645 | 651 | client.captureEvent({ message: 'hello' });
|
646 | 652 | expect(TestBackend.instance!.event).toBeUndefined();
|
| 653 | + expect(captureExceptionSpy).not.toBeCalled(); |
| 654 | + expect(loggerErrorSpy).toBeCalledWith(new SentryError('`beforeSend` returned `null`, will not send event.')); |
| 655 | + }); |
| 656 | + |
| 657 | + test('calls beforeSend and log info about invalid return value', () => { |
| 658 | + expect.assertions(3); |
| 659 | + const beforeSend = jest.fn(() => undefined); |
| 660 | + // @ts-ignore we need to test regular-js behavior |
| 661 | + const client = new TestClient({ dsn: PUBLIC_DSN, beforeSend }); |
| 662 | + const captureExceptionSpy = jest.spyOn(client, 'captureException'); |
| 663 | + const loggerErrorSpy = jest.spyOn(logger, 'error'); |
| 664 | + client.captureEvent({ message: 'hello' }); |
| 665 | + expect(TestBackend.instance!.event).toBeUndefined(); |
| 666 | + expect(captureExceptionSpy).not.toBeCalled(); |
| 667 | + expect(loggerErrorSpy).toBeCalledWith( |
| 668 | + new SentryError('`beforeSend` method has to return `null` or a valid event.'), |
| 669 | + ); |
647 | 670 | });
|
648 | 671 |
|
649 | 672 | test('calls async beforeSend and uses original event without any changes', done => {
|
@@ -718,6 +741,44 @@ describe('BaseClient', () => {
|
718 | 741 | expect(TestBackend.instance!.event!.message).toBe('hello');
|
719 | 742 | expect((TestBackend.instance!.event! as any).data).toBe('someRandomThing');
|
720 | 743 | });
|
| 744 | + |
| 745 | + test('eventProcessor can drop the even when it returns null', () => { |
| 746 | + expect.assertions(3); |
| 747 | + const client = new TestClient({ dsn: PUBLIC_DSN }); |
| 748 | + const captureExceptionSpy = jest.spyOn(client, 'captureException'); |
| 749 | + const loggerErrorSpy = jest.spyOn(logger, 'error'); |
| 750 | + const scope = new Scope(); |
| 751 | + scope.addEventProcessor(() => null); |
| 752 | + client.captureEvent({ message: 'hello' }, {}, scope); |
| 753 | + expect(TestBackend.instance!.event).toBeUndefined(); |
| 754 | + expect(captureExceptionSpy).not.toBeCalled(); |
| 755 | + expect(loggerErrorSpy).toBeCalledWith(new SentryError('An event processor returned null, will not send event.')); |
| 756 | + }); |
| 757 | + |
| 758 | + test('eventProcessor sends an event and logs when it crashes', () => { |
| 759 | + expect.assertions(3); |
| 760 | + const client = new TestClient({ dsn: PUBLIC_DSN }); |
| 761 | + const captureExceptionSpy = jest.spyOn(client, 'captureException'); |
| 762 | + const loggerErrorSpy = jest.spyOn(logger, 'error'); |
| 763 | + const scope = new Scope(); |
| 764 | + const exception = new Error('sorry'); |
| 765 | + scope.addEventProcessor(() => { |
| 766 | + throw exception; |
| 767 | + }); |
| 768 | + client.captureEvent({ message: 'hello' }, {}, scope); |
| 769 | + expect(TestBackend.instance!.event!.exception!.values![0]).toStrictEqual({ type: 'Error', value: 'sorry' }); |
| 770 | + expect(captureExceptionSpy).toBeCalledWith(exception, { |
| 771 | + data: { |
| 772 | + __sentry__: true, |
| 773 | + }, |
| 774 | + originalException: exception, |
| 775 | + }); |
| 776 | + expect(loggerErrorSpy).toBeCalledWith( |
| 777 | + new SentryError( |
| 778 | + `Event processing pipeline threw an error, original event will not be sent. Details have been sent as a new event.\nReason: ${exception}`, |
| 779 | + ), |
| 780 | + ); |
| 781 | + }); |
721 | 782 | });
|
722 | 783 |
|
723 | 784 | describe('integrations', () => {
|
|
0 commit comments