Skip to content
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
5 changes: 3 additions & 2 deletions packages/react/src/redux.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { addEventProcessor, getClient, getCurrentScope } from '@sentry/browser';
import { getClient, getCurrentScope, getGlobalScope } from '@sentry/core';
import type { Scope } from '@sentry/types';
import { addNonEnumerableProperty } from '@sentry/utils';

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

const scope = getCurrentScope();

/* Action breadcrumbs */
const transformedAction = options.actionTransformer(action);
if (typeof transformedAction !== 'undefined' && transformedAction !== null) {
Expand Down
32 changes: 18 additions & 14 deletions packages/react/test/redux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ import { createReduxEnhancer } from '../src/redux';

const mockAddBreadcrumb = jest.fn();
const mockSetContext = jest.fn();
const mockGlobalScopeAddEventProcessor = jest.fn();

jest.mock('@sentry/browser', () => ({
...jest.requireActual('@sentry/browser'),
jest.mock('@sentry/core', () => ({
...jest.requireActual('@sentry/core'),
getCurrentScope() {
return {
addBreadcrumb: mockAddBreadcrumb,
setContext: mockSetContext,
};
},
getGlobalScope() {
return {
addEventProcessor: mockGlobalScopeAddEventProcessor,
};
},
addEventProcessor: jest.fn(),
}));

const mockAddEventProcessor = Sentry.addEventProcessor as jest.Mock;

afterEach(() => {
mockAddBreadcrumb.mockReset();
mockSetContext.mockReset();
mockAddEventProcessor.mockReset();
mockGlobalScopeAddEventProcessor.mockReset();
});

describe('createReduxEnhancer', () => {
Expand Down Expand Up @@ -257,9 +261,9 @@ describe('createReduxEnhancer', () => {

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

expect(mockAddEventProcessor).toHaveBeenCalledTimes(1);
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(1);

const callbackFunction = mockAddEventProcessor.mock.calls[0][0];
const callbackFunction = mockGlobalScopeAddEventProcessor.mock.calls[0][0];

const mockEvent = {
contexts: {
Expand Down Expand Up @@ -306,7 +310,7 @@ describe('createReduxEnhancer', () => {

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

expect(mockAddEventProcessor).toHaveBeenCalledTimes(0);
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(0);
});

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

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

expect(mockAddEventProcessor).toHaveBeenCalledTimes(1);
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(1);

const callbackFunction = mockAddEventProcessor.mock.calls[0][0];
const callbackFunction = mockGlobalScopeAddEventProcessor.mock.calls[0][0];

const mockEvent = {
contexts: {
Expand Down Expand Up @@ -353,9 +357,9 @@ describe('createReduxEnhancer', () => {

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

expect(mockAddEventProcessor).toHaveBeenCalledTimes(1);
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(1);

const callbackFunction = mockAddEventProcessor.mock.calls[0][0];
const callbackFunction = mockGlobalScopeAddEventProcessor.mock.calls[0][0];

const mockEvent = {
contexts: {
Expand Down Expand Up @@ -385,9 +389,9 @@ describe('createReduxEnhancer', () => {

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

expect(mockAddEventProcessor).toHaveBeenCalledTimes(1);
expect(mockGlobalScopeAddEventProcessor).toHaveBeenCalledTimes(1);

const callbackFunction = mockAddEventProcessor.mock.calls[0][0];
const callbackFunction = mockGlobalScopeAddEventProcessor.mock.calls[0][0];

const mockEvent = {
type: 'not_redux',
Expand Down