Skip to content

feat(core): Allow to pass custom scope to captureFeedback() #12216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/core/src/feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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;
}
51 changes: 50 additions & 1 deletion packages/core/test/lib/feedback.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
});
});
Loading