Skip to content

Commit a6a58e3

Browse files
committed
fix(replay): Only use scope.getLastBreadcrumb if
available
1 parent d33b88c commit a6a58e3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/replay/src/coreHandlers/handleScope.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export const handleScopeListener: (replay: ReplayContainer) => (scope: Scope) =>
2626
* An event handler to handle scope changes.
2727
*/
2828
export function handleScope(scope: Scope): Breadcrumb | null {
29+
if (typeof scope.getLastBreadcrumb !== 'function') {
30+
return null;
31+
}
32+
2933
const newBreadcrumb = scope.getLastBreadcrumb();
3034

3135
// Listener can be called when breadcrumbs have not changed, so we store the

packages/replay/test/unit/coreHandlers/handleScope.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import type { Breadcrumb, Scope } from '@sentry/types';
33
import * as HandleScope from '../../../src/coreHandlers/handleScope';
44

55
describe('Unit | coreHandlers | handleScope', () => {
6-
const mockHandleScope = jest.spyOn(HandleScope, 'handleScope');
6+
let mockHandleScope: jest.SpyInstance;
7+
8+
beforeEach(() => {
9+
mockHandleScope = jest.spyOn(HandleScope, 'handleScope');
10+
mockHandleScope.mockClear();
11+
});
712

813
it('returns a breadcrumb only if last breadcrumb has changed', function () {
914
const scope = {
@@ -47,4 +52,11 @@ describe('Unit | coreHandlers | handleScope', () => {
4752
expect(mockHandleScope).toHaveBeenCalledTimes(1);
4853
expect(mockHandleScope).toHaveReturnedWith(expect.objectContaining({ message: 'f00', category: 'console' }));
4954
});
55+
56+
it('returns null if the method does not exist on the scope', () => {
57+
const scope = {} as unknown as Scope;
58+
HandleScope.handleScope(scope);
59+
expect(mockHandleScope).toHaveBeenCalledTimes(1);
60+
expect(mockHandleScope).toHaveReturnedWith(null);
61+
});
5062
});

0 commit comments

Comments
 (0)