Skip to content

fix(replay): Only call scope.getLastBreadcrumb if available #6969

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 2 commits into from
Jan 30, 2023
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
6 changes: 5 additions & 1 deletion packages/replay/src/coreHandlers/handleScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export const handleScopeListener: (replay: ReplayContainer) => (scope: Scope) =>
* An event handler to handle scope changes.
*/
export function handleScope(scope: Scope): Breadcrumb | null {
const newBreadcrumb = scope.getLastBreadcrumb();
// TODO (v8): Remove this guard. This was put in place because we introduced
// Scope.getLastBreadcrumb mid-v7 which caused incompatibilities with older SDKs.
// For now, we'll just return null if the method doesn't exist but we should eventually
// get rid of this guard.
const newBreadcrumb = scope.getLastBreadcrumb && scope.getLastBreadcrumb();

// Listener can be called when breadcrumbs have not changed, so we store the
// reference to the last crumb and only return a crumb if it has changed
Expand Down
14 changes: 13 additions & 1 deletion packages/replay/test/unit/coreHandlers/handleScope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type { Breadcrumb, Scope } from '@sentry/types';
import * as HandleScope from '../../../src/coreHandlers/handleScope';

describe('Unit | coreHandlers | handleScope', () => {
const mockHandleScope = jest.spyOn(HandleScope, 'handleScope');
let mockHandleScope: jest.SpyInstance;

beforeEach(() => {
mockHandleScope = jest.spyOn(HandleScope, 'handleScope');
mockHandleScope.mockClear();
});

it('returns a breadcrumb only if last breadcrumb has changed', function () {
const scope = {
Expand Down Expand Up @@ -47,4 +52,11 @@ describe('Unit | coreHandlers | handleScope', () => {
expect(mockHandleScope).toHaveBeenCalledTimes(1);
expect(mockHandleScope).toHaveReturnedWith(expect.objectContaining({ message: 'f00', category: 'console' }));
});

it('returns null if the method does not exist on the scope', () => {
const scope = {} as unknown as Scope;
HandleScope.handleScope(scope);
expect(mockHandleScope).toHaveBeenCalledTimes(1);
expect(mockHandleScope).toHaveReturnedWith(null);
});
});