Skip to content

Commit 2ce3e7d

Browse files
AleshaOlegLuca Forstner
andauthored
fix(react): Fix attachReduxState option (#10381)
Co-authored-by: Luca Forstner <[email protected]>
1 parent 8554dca commit 2ce3e7d

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

packages/react/src/redux.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { addEventProcessor, getClient, getCurrentScope } from '@sentry/browser';
2+
import { getClient, getCurrentScope, getGlobalScope } from '@sentry/core';
33
import type { Scope } from '@sentry/types';
44
import { addNonEnumerableProperty } from '@sentry/utils';
55

@@ -97,7 +97,7 @@ function createReduxEnhancer(enhancerOptions?: Partial<SentryEnhancerOptions>):
9797
return (next: StoreEnhancerStoreCreator): StoreEnhancerStoreCreator =>
9898
<S = any, A extends Action = AnyAction>(reducer: Reducer<S, A>, initialState?: PreloadedState<S>) => {
9999
options.attachReduxState &&
100-
addEventProcessor((event, hint) => {
100+
getGlobalScope().addEventProcessor((event, hint) => {
101101
try {
102102
// @ts-expect-error try catch to reduce bundle size
103103
if (event.type === undefined && event.contexts.state.state.type === 'redux') {
@@ -117,6 +117,7 @@ function createReduxEnhancer(enhancerOptions?: Partial<SentryEnhancerOptions>):
117117
const newState = reducer(state, action);
118118

119119
const scope = getCurrentScope();
120+
120121
/* Action breadcrumbs */
121122
const transformedAction = options.actionTransformer(action);
122123
if (typeof transformedAction !== 'undefined' && transformedAction !== null) {

packages/react/test/redux.test.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@ import { createReduxEnhancer } from '../src/redux';
55

66
const mockAddBreadcrumb = jest.fn();
77
const mockSetContext = jest.fn();
8+
const mockGlobalScopeAddEventProcessor = jest.fn();
89

9-
jest.mock('@sentry/browser', () => ({
10-
...jest.requireActual('@sentry/browser'),
10+
jest.mock('@sentry/core', () => ({
11+
...jest.requireActual('@sentry/core'),
1112
getCurrentScope() {
1213
return {
1314
addBreadcrumb: mockAddBreadcrumb,
1415
setContext: mockSetContext,
1516
};
1617
},
18+
getGlobalScope() {
19+
return {
20+
addEventProcessor: mockGlobalScopeAddEventProcessor,
21+
};
22+
},
1723
addEventProcessor: jest.fn(),
1824
}));
1925

20-
const mockAddEventProcessor = Sentry.addEventProcessor as jest.Mock;
21-
2226
afterEach(() => {
2327
mockAddBreadcrumb.mockReset();
2428
mockSetContext.mockReset();
25-
mockAddEventProcessor.mockReset();
29+
mockGlobalScopeAddEventProcessor.mockReset();
2630
});
2731

2832
describe('createReduxEnhancer', () => {
@@ -257,9 +261,9 @@ describe('createReduxEnhancer', () => {
257261

258262
Redux.createStore((state = initialState) => state, enhancer);
259263

260-
expect(mockAddEventProcessor).toHaveBeenCalledTimes(1);
264+
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(1);
261265

262-
const callbackFunction = mockAddEventProcessor.mock.calls[0][0];
266+
const callbackFunction = mockGlobalScopeAddEventProcessor.mock.calls[0][0];
263267

264268
const mockEvent = {
265269
contexts: {
@@ -306,7 +310,7 @@ describe('createReduxEnhancer', () => {
306310

307311
Redux.createStore((state = initialState) => state, enhancer);
308312

309-
expect(mockAddEventProcessor).toHaveBeenCalledTimes(0);
313+
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(0);
310314
});
311315

312316
it('does not attach when state.type is not redux', () => {
@@ -318,9 +322,9 @@ describe('createReduxEnhancer', () => {
318322

319323
Redux.createStore((state = initialState) => state, enhancer);
320324

321-
expect(mockAddEventProcessor).toHaveBeenCalledTimes(1);
325+
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(1);
322326

323-
const callbackFunction = mockAddEventProcessor.mock.calls[0][0];
327+
const callbackFunction = mockGlobalScopeAddEventProcessor.mock.calls[0][0];
324328

325329
const mockEvent = {
326330
contexts: {
@@ -353,9 +357,9 @@ describe('createReduxEnhancer', () => {
353357

354358
Redux.createStore((state = initialState) => state, enhancer);
355359

356-
expect(mockAddEventProcessor).toHaveBeenCalledTimes(1);
360+
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(1);
357361

358-
const callbackFunction = mockAddEventProcessor.mock.calls[0][0];
362+
const callbackFunction = mockGlobalScopeAddEventProcessor.mock.calls[0][0];
359363

360364
const mockEvent = {
361365
contexts: {
@@ -385,9 +389,9 @@ describe('createReduxEnhancer', () => {
385389

386390
Redux.createStore((state = initialState) => state, enhancer);
387391

388-
expect(mockAddEventProcessor).toHaveBeenCalledTimes(1);
392+
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(1);
389393

390-
const callbackFunction = mockAddEventProcessor.mock.calls[0][0];
394+
const callbackFunction = mockGlobalScopeAddEventProcessor.mock.calls[0][0];
391395

392396
const mockEvent = {
393397
type: 'not_redux',

0 commit comments

Comments
 (0)