From fe823cd12ce2549afd6e165b09a1150522d231ac Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 24 May 2024 15:40:22 +0200 Subject: [PATCH] feat(core): Allow to pass custom scope to `captureFeedback()` --- packages/core/src/feedback.ts | 7 ++-- packages/core/test/lib/feedback.test.ts | 51 ++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/packages/core/src/feedback.ts b/packages/core/src/feedback.ts index ae3abc7ca50f..9bd583a288bb 100644 --- a/packages/core/src/feedback.ts +++ b/packages/core/src/feedback.ts @@ -8,11 +8,10 @@ import { getClient, getCurrentScope } from './currentScopes'; export function captureFeedback( feedbackParams: SendFeedbackParams, hint: EventHint & { includeReplay?: boolean } = {}, + scope = getCurrentScope(), ): string { const { message, name, email, url, source, associatedEventId } = feedbackParams; - const client = getClient(); - const feedbackEvent: FeedbackEvent = { contexts: { feedback: dropUndefinedKeys({ @@ -28,11 +27,13 @@ export function captureFeedback( level: 'info', }; + const client = (scope && scope.getClient()) || getClient(); + if (client) { client.emit('beforeSendFeedback', feedbackEvent, hint); } - const eventId = getCurrentScope().captureEvent(feedbackEvent, hint); + const eventId = scope.captureEvent(feedbackEvent, hint); return eventId; } diff --git a/packages/core/test/lib/feedback.test.ts b/packages/core/test/lib/feedback.test.ts index faa17a8c51ea..e67521bfa2f8 100644 --- a/packages/core/test/lib/feedback.test.ts +++ b/packages/core/test/lib/feedback.test.ts @@ -1,5 +1,13 @@ import type { Span } from '@sentry/types'; -import { addBreadcrumb, getCurrentScope, setCurrentClient, startSpan, withIsolationScope, withScope } from '../../src'; +import { + Scope, + addBreadcrumb, + getCurrentScope, + setCurrentClient, + startSpan, + withIsolationScope, + withScope, +} from '../../src'; import { captureFeedback } from '../../src/feedback'; import { TestClient, getDefaultTestClientOptions } from '../mocks/client'; @@ -448,4 +456,45 @@ describe('captureFeedback', () => { ], ]); }); + + test('it allows to pass a custom client', async () => { + const client = new TestClient( + getDefaultTestClientOptions({ + dsn: 'https://dsn@ingest.f00.f00/1', + enableSend: true, + }), + ); + setCurrentClient(client); + client.init(); + + const client2 = new TestClient( + getDefaultTestClientOptions({ + dsn: 'https://dsn@ingest.f00.f00/1', + enableSend: true, + defaultIntegrations: false, + }), + ); + client2.init(); + const scope = new Scope(); + scope.setClient(client2); + + const mockTransport = jest.spyOn(client.getTransport()!, 'send'); + const mockTransport2 = jest.spyOn(client2.getTransport()!, 'send'); + + const eventId = captureFeedback( + { + message: 'test', + }, + {}, + scope, + ); + + await client.flush(); + await client2.flush(); + + expect(typeof eventId).toBe('string'); + + expect(mockTransport).not.toHaveBeenCalled(); + expect(mockTransport2).toHaveBeenCalledTimes(1); + }); });