Skip to content

Commit fa10cf6

Browse files
committed
fix tests
1 parent 64a14b6 commit fa10cf6

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

packages/react/src/profiler.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,15 @@ class Profiler extends React.Component<ProfilerProps> {
8282
const changedProps = Object.keys(updateProps).filter(k => updateProps[k] !== this.props.updateProps[k]);
8383
if (changedProps.length > 0 && !this._updateSpan) {
8484
const now = timestampInSeconds();
85-
const scope = new Scope();
86-
scope.setSpan(this._mountSpan);
85+
const scope = new Scope().setSpan(this._mountSpan);
8786
this._updateSpan = startInactiveSpan({
8887
scope,
8988
name: `<${this.props.name}>`,
9089
op: REACT_UPDATE_OP,
9190
origin: 'auto.ui.react.profiler',
9291
startTimestamp: now,
9392
attributes: {
94-
changedProps,
93+
'ui.react.changed_props': changedProps,
9594
'ui.component_name': this.props.name,
9695
},
9796
});
@@ -206,8 +205,7 @@ function useProfiler(
206205
return (): void => {
207206
const endTime = timestampInSeconds();
208207
if (mountSpan && options.hasRenderSpan) {
209-
const scope = new Scope();
210-
scope.setSpan(mountSpan);
208+
const scope = new Scope().setSpan(mountSpan);
211209
const renderSpan = startInactiveSpan({
212210
scope,
213211
name: `<${name}>`,

packages/react/test/profiler.test.tsx

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Scope } from '@sentry/browser';
12
import type { SpanContext } from '@sentry/types';
23
import { render } from '@testing-library/react';
34
import { renderHook } from '@testing-library/react-hooks';
@@ -14,31 +15,26 @@ const mockFinish = jest.fn();
1415
class MockSpan {
1516
public constructor(public readonly ctx: SpanContext) {}
1617

17-
public startChild(ctx: SpanContext): MockSpan {
18-
mockStartChild(ctx);
19-
return new MockSpan(ctx);
20-
}
21-
2218
public end(): void {
2319
mockFinish();
2420
}
2521
}
2622

27-
let activeTransaction: Record<string, any>;
23+
let activeSpan: Record<string, any>;
2824

2925
jest.mock('@sentry/browser', () => ({
30-
getCurrentHub: () => ({
31-
getIntegration: () => undefined,
32-
getScope: () => ({
33-
getTransaction: () => activeTransaction,
34-
}),
35-
}),
26+
...jest.requireActual('@sentry/browser'),
27+
getActiveSpan: () => activeSpan,
28+
startInactiveSpan: (ctx: SpanContext) => {
29+
mockStartChild(ctx);
30+
return new MockSpan(ctx);
31+
},
3632
}));
3733

3834
beforeEach(() => {
3935
mockStartChild.mockClear();
4036
mockFinish.mockClear();
41-
activeTransaction = new MockSpan({ op: 'pageload' });
37+
activeSpan = new MockSpan({ op: 'pageload' });
4238
});
4339

4440
describe('withProfiler', () => {
@@ -78,10 +74,10 @@ describe('withProfiler', () => {
7874

7975
expect(mockStartChild).toHaveBeenCalledTimes(1);
8076
expect(mockStartChild).toHaveBeenLastCalledWith({
81-
description: `<${UNKNOWN_COMPONENT}>`,
77+
name: `<${UNKNOWN_COMPONENT}>`,
8278
op: REACT_MOUNT_OP,
8379
origin: 'auto.ui.react.profiler',
84-
data: { 'ui.component_name': 'unknown' },
80+
attributes: { 'ui.component_name': 'unknown' },
8581
});
8682
});
8783
});
@@ -96,12 +92,11 @@ describe('withProfiler', () => {
9692

9793
expect(mockStartChild).toHaveBeenCalledTimes(2);
9894
expect(mockStartChild).toHaveBeenLastCalledWith({
99-
description: `<${UNKNOWN_COMPONENT}>`,
100-
endTimestamp: expect.any(Number),
95+
name: `<${UNKNOWN_COMPONENT}>`,
10196
op: REACT_RENDER_OP,
10297
origin: 'auto.ui.react.profiler',
10398
startTimestamp: undefined,
104-
data: { 'ui.component_name': 'unknown' },
99+
attributes: { 'ui.component_name': 'unknown' },
105100
});
106101
});
107102

@@ -128,22 +123,24 @@ describe('withProfiler', () => {
128123
rerender(<ProfiledComponent num={1} />);
129124
expect(mockStartChild).toHaveBeenCalledTimes(2);
130125
expect(mockStartChild).toHaveBeenLastCalledWith({
131-
data: { changedProps: ['num'], 'ui.component_name': 'unknown' },
132-
description: `<${UNKNOWN_COMPONENT}>`,
126+
attributes: { 'ui.react.changed_props': ['num'], 'ui.component_name': 'unknown' },
127+
name: `<${UNKNOWN_COMPONENT}>`,
133128
op: REACT_UPDATE_OP,
134129
origin: 'auto.ui.react.profiler',
135130
startTimestamp: expect.any(Number),
131+
scope: expect.any(Scope),
136132
});
137133
expect(mockFinish).toHaveBeenCalledTimes(2);
138134
// New props yet again
139135
rerender(<ProfiledComponent num={2} />);
140136
expect(mockStartChild).toHaveBeenCalledTimes(3);
141137
expect(mockStartChild).toHaveBeenLastCalledWith({
142-
data: { changedProps: ['num'], 'ui.component_name': 'unknown' },
143-
description: `<${UNKNOWN_COMPONENT}>`,
138+
attributes: { 'ui.react.changed_props': ['num'], 'ui.component_name': 'unknown' },
139+
name: `<${UNKNOWN_COMPONENT}>`,
144140
op: REACT_UPDATE_OP,
145141
origin: 'auto.ui.react.profiler',
146142
startTimestamp: expect.any(Number),
143+
scope: expect.any(Scope),
147144
});
148145
expect(mockFinish).toHaveBeenCalledTimes(3);
149146

@@ -179,10 +176,10 @@ describe('useProfiler()', () => {
179176

180177
expect(mockStartChild).toHaveBeenCalledTimes(1);
181178
expect(mockStartChild).toHaveBeenLastCalledWith({
182-
description: '<Example>',
179+
name: '<Example>',
183180
op: REACT_MOUNT_OP,
184181
origin: 'auto.ui.react.profiler',
185-
data: { 'ui.component_name': 'Example' },
182+
attributes: { 'ui.component_name': 'Example' },
186183
});
187184
});
188185
});
@@ -203,10 +200,10 @@ describe('useProfiler()', () => {
203200
expect(mockStartChild).toHaveBeenCalledTimes(2);
204201
expect(mockStartChild).toHaveBeenLastCalledWith(
205202
expect.objectContaining({
206-
description: '<Example>',
203+
name: '<Example>',
207204
op: REACT_RENDER_OP,
208205
origin: 'auto.ui.react.profiler',
209-
data: { 'ui.component_name': 'Example' },
206+
attributes: { 'ui.component_name': 'Example' },
210207
}),
211208
);
212209
});

0 commit comments

Comments
 (0)