Skip to content

Commit 5a1fae6

Browse files
authored
feat(core): Allow to pass custom scope to captureFeedback() (#12216)
This allows to pass a custom scope to `Sentry.captureFeedback()`, like this: ```js Sentry.captureFeedback({message : 'test' }, {}, scope); ``` This should fix the use case from #11072 (comment)
1 parent cc1cb7b commit 5a1fae6

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

packages/core/src/feedback.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import { getClient, getCurrentScope } from './currentScopes';
88
export function captureFeedback(
99
feedbackParams: SendFeedbackParams,
1010
hint: EventHint & { includeReplay?: boolean } = {},
11+
scope = getCurrentScope(),
1112
): string {
1213
const { message, name, email, url, source, associatedEventId } = feedbackParams;
1314

14-
const client = getClient();
15-
1615
const feedbackEvent: FeedbackEvent = {
1716
contexts: {
1817
feedback: dropUndefinedKeys({
@@ -28,11 +27,13 @@ export function captureFeedback(
2827
level: 'info',
2928
};
3029

30+
const client = (scope && scope.getClient()) || getClient();
31+
3132
if (client) {
3233
client.emit('beforeSendFeedback', feedbackEvent, hint);
3334
}
3435

35-
const eventId = getCurrentScope().captureEvent(feedbackEvent, hint);
36+
const eventId = scope.captureEvent(feedbackEvent, hint);
3637

3738
return eventId;
3839
}

packages/core/test/lib/feedback.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { Span } from '@sentry/types';
2-
import { addBreadcrumb, getCurrentScope, setCurrentClient, startSpan, withIsolationScope, withScope } from '../../src';
2+
import {
3+
Scope,
4+
addBreadcrumb,
5+
getCurrentScope,
6+
setCurrentClient,
7+
startSpan,
8+
withIsolationScope,
9+
withScope,
10+
} from '../../src';
311
import { captureFeedback } from '../../src/feedback';
412
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';
513

@@ -448,4 +456,45 @@ describe('captureFeedback', () => {
448456
],
449457
]);
450458
});
459+
460+
test('it allows to pass a custom client', async () => {
461+
const client = new TestClient(
462+
getDefaultTestClientOptions({
463+
dsn: 'https://dsn@ingest.f00.f00/1',
464+
enableSend: true,
465+
}),
466+
);
467+
setCurrentClient(client);
468+
client.init();
469+
470+
const client2 = new TestClient(
471+
getDefaultTestClientOptions({
472+
dsn: 'https://dsn@ingest.f00.f00/1',
473+
enableSend: true,
474+
defaultIntegrations: false,
475+
}),
476+
);
477+
client2.init();
478+
const scope = new Scope();
479+
scope.setClient(client2);
480+
481+
const mockTransport = jest.spyOn(client.getTransport()!, 'send');
482+
const mockTransport2 = jest.spyOn(client2.getTransport()!, 'send');
483+
484+
const eventId = captureFeedback(
485+
{
486+
message: 'test',
487+
},
488+
{},
489+
scope,
490+
);
491+
492+
await client.flush();
493+
await client2.flush();
494+
495+
expect(typeof eventId).toBe('string');
496+
497+
expect(mockTransport).not.toHaveBeenCalled();
498+
expect(mockTransport2).toHaveBeenCalledTimes(1);
499+
});
451500
});

0 commit comments

Comments
 (0)