Skip to content

Commit 2155da1

Browse files
Mock hub stopped working as JS SDK internals changed
getsentry/sentry-javascript#7339
1 parent e1271ae commit 2155da1

File tree

1 file changed

+115
-76
lines changed

1 file changed

+115
-76
lines changed

test/tracing/stalltracking.test.ts

Lines changed: 115 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Hub } from '@sentry/core';
12
import { IdleTransaction, Transaction } from '@sentry/tracing';
23
import type { Event } from '@sentry/types';
34

@@ -8,15 +9,6 @@ const mockHub = {
89
getClient: jest.fn(),
910
};
1011

11-
jest.mock('@sentry/core', () => {
12-
const hubOriginal = jest.requireActual('@sentry/core');
13-
14-
return {
15-
...hubOriginal,
16-
getCurrentHub: () => mockHub,
17-
};
18-
});
19-
2012
const getLastEvent = (): Event => {
2113
return mockHub.captureEvent.mock.calls[mockHub.captureEvent.mock.calls.length - 1][0];
2214
};
@@ -37,13 +29,18 @@ beforeEach(() => {
3729
});
3830

3931
describe('StallTracking', () => {
32+
const localHub: Hub = mockHub as unknown as Hub;
33+
4034
it('Stall tracking detects a JS stall', (done) => {
4135
const stallTracking = new StallTrackingInstrumentation();
4236

43-
const transaction = new Transaction({
44-
name: 'Test Transaction',
45-
sampled: true,
46-
});
37+
const transaction = new Transaction(
38+
{
39+
name: 'Test Transaction',
40+
sampled: true,
41+
},
42+
localHub,
43+
);
4744
transaction.initSpanRecorder();
4845

4946
stallTracking.onTransactionStart(transaction);
@@ -75,10 +72,13 @@ describe('StallTracking', () => {
7572
it('Stall tracking detects multiple JS stalls', (done) => {
7673
const stallTracking = new StallTrackingInstrumentation();
7774

78-
const transaction = new Transaction({
79-
name: 'Test Transaction',
80-
sampled: true,
81-
});
75+
const transaction = new Transaction(
76+
{
77+
name: 'Test Transaction',
78+
sampled: true,
79+
},
80+
localHub,
81+
);
8282
transaction.initSpanRecorder();
8383

8484
stallTracking.onTransactionStart(transaction);
@@ -108,10 +108,13 @@ describe('StallTracking', () => {
108108
it('Stall tracking timeout is stopped after finishing all transactions (single)', () => {
109109
const stallTracking = new StallTrackingInstrumentation();
110110

111-
const transaction = new Transaction({
112-
name: 'Test Transaction',
113-
sampled: true,
114-
});
111+
const transaction = new Transaction(
112+
{
113+
name: 'Test Transaction',
114+
sampled: true,
115+
},
116+
localHub,
117+
);
115118

116119
stallTracking.onTransactionStart(transaction);
117120

@@ -128,18 +131,27 @@ describe('StallTracking', () => {
128131
it('Stall tracking timeout is stopped after finishing all transactions (multiple)', (done) => {
129132
const stallTracking = new StallTrackingInstrumentation();
130133

131-
const transaction0 = new Transaction({
132-
name: 'Test Transaction 0',
133-
sampled: true,
134-
});
135-
const transaction1 = new Transaction({
136-
name: 'Test Transaction 1',
137-
sampled: true,
138-
});
139-
const transaction2 = new Transaction({
140-
name: 'Test Transaction 2',
141-
sampled: true,
142-
});
134+
const transaction0 = new Transaction(
135+
{
136+
name: 'Test Transaction 0',
137+
sampled: true,
138+
},
139+
localHub,
140+
);
141+
const transaction1 = new Transaction(
142+
{
143+
name: 'Test Transaction 1',
144+
sampled: true,
145+
},
146+
localHub,
147+
);
148+
const transaction2 = new Transaction(
149+
{
150+
name: 'Test Transaction 2',
151+
sampled: true,
152+
},
153+
localHub,
154+
);
143155

144156
stallTracking.onTransactionStart(transaction0);
145157
stallTracking.onTransactionStart(transaction1);
@@ -177,10 +189,13 @@ describe('StallTracking', () => {
177189
it('Stall tracking returns measurements format on finish', () => {
178190
const stallTracking = new StallTrackingInstrumentation();
179191

180-
const transaction = new Transaction({
181-
name: 'Test Transaction',
182-
sampled: true,
183-
});
192+
const transaction = new Transaction(
193+
{
194+
name: 'Test Transaction',
195+
sampled: true,
196+
},
197+
localHub,
198+
);
184199

185200
stallTracking.onTransactionStart(transaction);
186201

@@ -200,10 +215,13 @@ describe('StallTracking', () => {
200215
it("Stall tracking returns null on a custom endTimestamp that is not a span's", () => {
201216
const stallTracking = new StallTrackingInstrumentation();
202217

203-
const transaction = new Transaction({
204-
name: 'Test Transaction',
205-
sampled: true,
206-
});
218+
const transaction = new Transaction(
219+
{
220+
name: 'Test Transaction',
221+
sampled: true,
222+
},
223+
localHub,
224+
);
207225

208226
stallTracking.onTransactionStart(transaction);
209227

@@ -217,11 +235,14 @@ describe('StallTracking', () => {
217235
it('Stall tracking supports endTimestamp that is from the last span (trimEnd case)', (done) => {
218236
const stallTracking = new StallTrackingInstrumentation();
219237

220-
const transaction = new Transaction({
221-
name: 'Test Transaction',
222-
trimEnd: true,
223-
sampled: true,
224-
});
238+
const transaction = new Transaction(
239+
{
240+
name: 'Test Transaction',
241+
trimEnd: true,
242+
sampled: true,
243+
},
244+
localHub,
245+
);
225246
transaction.initSpanRecorder();
226247

227248
stallTracking.onTransactionStart(transaction);
@@ -262,11 +283,14 @@ describe('StallTracking', () => {
262283
it('Stall tracking rejects endTimestamp that is from the last span if trimEnd is false (trimEnd case)', (done) => {
263284
const stallTracking = new StallTrackingInstrumentation();
264285

265-
const transaction = new Transaction({
266-
name: 'Test Transaction',
267-
trimEnd: false,
268-
sampled: true,
269-
});
286+
const transaction = new Transaction(
287+
{
288+
name: 'Test Transaction',
289+
trimEnd: false,
290+
sampled: true,
291+
},
292+
localHub,
293+
);
270294
transaction.initSpanRecorder();
271295

272296
stallTracking.onTransactionStart(transaction);
@@ -299,10 +323,13 @@ describe('StallTracking', () => {
299323
it('Stall tracking rejects endTimestamp even if it is a span time (custom endTimestamp case)', (done) => {
300324
const stallTracking = new StallTrackingInstrumentation();
301325

302-
const transaction = new Transaction({
303-
name: 'Test Transaction',
304-
sampled: true,
305-
});
326+
const transaction = new Transaction(
327+
{
328+
name: 'Test Transaction',
329+
sampled: true,
330+
},
331+
localHub,
332+
);
306333
transaction.initSpanRecorder();
307334

308335
stallTracking.onTransactionStart(transaction);
@@ -338,13 +365,16 @@ describe('StallTracking', () => {
338365
it('Stall tracking supports idleTransaction with unfinished spans', async () => {
339366
jest.useFakeTimers();
340367
const stallTracking = new StallTrackingInstrumentation();
341-
const localHub = mockHub;
342-
const idleTransaction = new IdleTransaction({
343-
name: 'Test Transaction',
344-
trimEnd: true,
345-
sampled: true,
346-
// @ts-ignore use the mocked hub.
347-
}, localHub, undefined, undefined);
368+
const idleTransaction = new IdleTransaction(
369+
{
370+
name: 'Test Transaction',
371+
trimEnd: true,
372+
sampled: true,
373+
},
374+
localHub,
375+
undefined,
376+
undefined,
377+
);
348378
idleTransaction.initSpanRecorder();
349379

350380
stallTracking.onTransactionStart(idleTransaction);
@@ -381,11 +411,14 @@ describe('StallTracking', () => {
381411
it('Stall tracking ignores unfinished spans in normal transactions', (done) => {
382412
const stallTracking = new StallTrackingInstrumentation();
383413

384-
const transaction = new Transaction({
385-
name: 'Test Transaction',
386-
trimEnd: true,
387-
sampled: true,
388-
});
414+
const transaction = new Transaction(
415+
{
416+
name: 'Test Transaction',
417+
trimEnd: true,
418+
sampled: true,
419+
},
420+
localHub,
421+
);
389422
transaction.initSpanRecorder();
390423

391424
stallTracking.onTransactionStart(transaction);
@@ -426,11 +459,14 @@ describe('StallTracking', () => {
426459
it('Stall tracking only measures stalls inside the final time when trimEnd is used', (done) => {
427460
const stallTracking = new StallTrackingInstrumentation();
428461

429-
const transaction = new Transaction({
430-
name: 'Test Transaction',
431-
trimEnd: true,
432-
sampled: true,
433-
});
462+
const transaction = new Transaction(
463+
{
464+
name: 'Test Transaction',
465+
trimEnd: true,
466+
sampled: true,
467+
},
468+
localHub,
469+
);
434470
transaction.initSpanRecorder();
435471

436472
stallTracking.onTransactionStart(transaction);
@@ -474,10 +510,13 @@ describe('StallTracking', () => {
474510
const stallTracking = new StallTrackingInstrumentation();
475511

476512
const transactions = new Array(11).fill(0).map((_, i) => {
477-
const transaction = new Transaction({
478-
name: `Test Transaction ${i}`,
479-
sampled: true,
480-
});
513+
const transaction = new Transaction(
514+
{
515+
name: `Test Transaction ${i}`,
516+
sampled: true,
517+
},
518+
localHub,
519+
);
481520

482521
stallTracking.onTransactionStart(transaction);
483522

0 commit comments

Comments
 (0)