Skip to content

Commit c9098d7

Browse files
committed
Fixes fake timers jest issue after getsentry/sentry-javascript#16890
1 parent 6ae0d42 commit c9098d7

11 files changed

+64
-16
lines changed

packages/core/test/profiling/integration.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ describe('profiling integration', () => {
4242
type: 'sourcemap',
4343
},
4444
]);
45-
jest.useFakeTimers();
45+
jest.useFakeTimers({
46+
advanceTimers: true,
47+
doNotFake: ['performance'], // Keep real performance API
48+
});
4649
});
4750

4851
afterEach(async () => {

packages/core/test/tracing/gesturetracing.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ describe('GestureTracing', () => {
5454

5555
beforeEach(() => {
5656
jest.clearAllMocks();
57-
jest.useFakeTimers();
57+
jest.useFakeTimers({
58+
advanceTimers: true,
59+
doNotFake: ['performance'], // Keep real performance API
60+
});
5861
client = setupTestClient({
5962
enableUserInteractionTracing: true,
6063
});

packages/core/test/tracing/integrations/nativeframes.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ jest.mock('../../../src/js/wrapper', () => {
1616
};
1717
});
1818

19-
jest.useFakeTimers({ advanceTimers: true });
19+
jest.useFakeTimers({
20+
advanceTimers: true,
21+
doNotFake: ['performance'], // Keep real performance API
22+
});
2023

2124
const mockDate = new Date(2024, 7, 14); // Set your desired mock date here
2225
const originalDateNow = Date.now; // Store the original Date.now function

packages/core/test/tracing/integrations/stallTracking/stalltracking.background.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ describe('BackgroundEventListener', () => {
2020
stallTracking['_internalState']['lastIntervalMs'] = currentTime;
2121
stallTracking['_internalState']['timeout'] = setTimeout(() => {}, LOOP_TIMEOUT_INTERVAL_MS); // Create a fake timeout to simulate a running interval
2222
stallTracking['_internalState']['isBackground'] = true;
23-
jest.useFakeTimers(); // Enable fake timers to control timeouts
23+
jest.useFakeTimers({
24+
advanceTimers: true,
25+
doNotFake: ['performance'], // Keep real performance API
26+
});
2427
stallTracking['_internalState']['backgroundEventListener']('active' as AppStateStatus);
2528
// Check if _isBackground is set to false and _lastIntervalMs is updated correctly
2629
expect(stallTracking['_internalState']['isBackground']).toBe(false);
@@ -39,7 +42,10 @@ describe('BackgroundEventListener', () => {
3942
stallTracking['_internalState']['timeout'] = null;
4043
// Mock _iteration
4144
stallTracking['_internalState']['iteration'] = jest.fn();
42-
jest.useFakeTimers(); // Enable fake timers to control timeouts
45+
jest.useFakeTimers({
46+
advanceTimers: true,
47+
doNotFake: ['performance'], // Keep real performance API
48+
});
4349
stallTracking['_internalState']['backgroundEventListener']('active' as AppStateStatus);
4450

4551
expect(stallTracking['_internalState']['iteration']).not.toBeCalled();
@@ -49,7 +55,10 @@ describe('BackgroundEventListener', () => {
4955
stallTracking['_internalState']['timeout'] = setTimeout(() => {}, 500);
5056
// Mock _iteration
5157
stallTracking['_internalState']['iteration'] = jest.fn(); // Create a fake timeout to simulate a running interval
52-
jest.useFakeTimers(); // Enable fake timers to control timeouts
58+
jest.useFakeTimers({
59+
advanceTimers: true,
60+
doNotFake: ['performance'], // Keep real performance API
61+
});
5362
stallTracking['_internalState']['backgroundEventListener']('active' as AppStateStatus);
5463
expect(stallTracking['_internalState']['iteration']).toBeCalled();
5564
});

packages/core/test/tracing/integrations/stallTracking/stalltracking.iteration.test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ describe('Iteration', () => {
1919
stallTracking['isTracking'] = false;
2020
stallTracking['_internalState']['isBackground'] = false;
2121
stallTracking['_internalState']['lastIntervalMs'] = Date.now() - 1000; // Force a timeout
22-
jest.useFakeTimers();
22+
jest.useFakeTimers({
23+
advanceTimers: true,
24+
doNotFake: ['performance'], // Keep real performance API
25+
});
2326
// Invokes the private _interaction function.
2427
stallTracking['_internalState']['iteration']();
2528
expect(stallTracking['_internalState']['timeout']).toBeNull();
@@ -29,7 +32,10 @@ describe('Iteration', () => {
2932
stallTracking['isTracking'] = true;
3033
stallTracking['_internalState']['isBackground'] = true;
3134
stallTracking['_internalState']['lastIntervalMs'] = Date.now() - 1000; // Force a timeout
32-
jest.useFakeTimers();
35+
jest.useFakeTimers({
36+
advanceTimers: true,
37+
doNotFake: ['performance'], // Keep real performance API
38+
});
3339
// Invokes the private _interaction function.
3440
stallTracking['_internalState']['iteration']();
3541
expect(stallTracking['_internalState']['timeout']).toBeNull();
@@ -38,7 +44,10 @@ describe('Iteration', () => {
3844
const stallTracking = stallTrackingIntegration() as StallTrackingWithTestProperties;
3945
stallTracking['isTracking'] = true;
4046
stallTracking['_internalState']['isBackground'] = false;
41-
jest.useFakeTimers();
47+
jest.useFakeTimers({
48+
advanceTimers: true,
49+
doNotFake: ['performance'], // Keep real performance API
50+
});
4251
stallTracking['_internalState']['lastIntervalMs'] = Date.now(); // Force a timeout
4352
// Invokes the private _interaction function.
4453
stallTracking['_internalState']['iteration']();
@@ -50,7 +59,10 @@ describe('Iteration', () => {
5059
const _minimumStallThreshold = 100;
5160
// Call _iteration with totalTimeTaken >= LOOP_TIMEOUT_INTERVAL_MS + _minimumStallThreshold
5261
const totalTimeTaken = LOOP_TIMEOUT_INTERVAL_MS + _minimumStallThreshold;
53-
jest.useFakeTimers();
62+
jest.useFakeTimers({
63+
advanceTimers: true,
64+
doNotFake: ['performance'], // Keep real performance API
65+
});
5466
stallTracking['_internalState']['lastIntervalMs'] = Date.now() - totalTimeTaken;
5567
stallTracking['_internalState']['statsByTransaction'] = new Map();
5668
stallTracking['_internalState']['iteration']();

packages/core/test/tracing/integrations/stallTracking/stalltracking.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import { stallTrackingIntegration } from '../../../../src/js/tracing/integration
1313
import { getDefaultTestClientOptions, TestClient } from '../../../mocks/client';
1414
import { expectNonZeroStallMeasurements, expectStallMeasurements } from './stalltrackingutils';
1515

16-
jest.useFakeTimers({ advanceTimers: true });
16+
jest.useFakeTimers({
17+
advanceTimers: true,
18+
doNotFake: ['Date', 'performance'] // Keep real Date/performance APIs
19+
});
1720

1821
const expensiveOperation = () => {
1922
const expensiveObject: { value: string[] } = {

packages/core/test/tracing/integrations/userInteraction.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ describe('User Interaction Tracing', () => {
6262
let mockedUserInteractionId: { elementId: string | undefined; op: string };
6363

6464
beforeEach(() => {
65-
jest.useFakeTimers();
65+
jest.useFakeTimers({
66+
advanceTimers: true,
67+
doNotFake: ['performance'], // Keep real performance API
68+
});
6669
NATIVE.enableNative = true;
6770
mockedAppState.isAvailable = true;
6871
mockedAppState.currentState = 'active';

packages/core/test/tracing/reactnativenavigation.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ interface MockEventsRegistry extends EventsRegistry {
4343
onBottomTabPressed(event: BottomTabPressedEvent): void;
4444
}
4545

46-
jest.useFakeTimers({ advanceTimers: true });
46+
jest.useFakeTimers({
47+
advanceTimers: true,
48+
doNotFake: ['performance'], // Keep real performance API
49+
});
4750

4851
describe('React Native Navigation Instrumentation', () => {
4952
let mockEventsRegistry: MockEventsRegistry;

packages/core/test/tracing/reactnavigation.stalltracking.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import { getDefaultTestClientOptions, TestClient } from '../mocks/client';
1212
import { expectStallMeasurements } from './integrations/stallTracking/stalltrackingutils';
1313
import { createMockNavigationAndAttachTo } from './reactnavigationutils';
1414

15-
jest.useFakeTimers({ advanceTimers: 1 });
15+
jest.useFakeTimers({
16+
advanceTimers: true,
17+
doNotFake: ['performance'], // Keep real performance API
18+
});
1619

1720
describe('StallTracking with ReactNavigation', () => {
1821
let client: TestClient;

packages/core/test/tracing/reactnavigation.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ jest.mock('../../src/js/wrapper.ts', () => jest.requireActual('../mockWrapper.ts
4343
jest.mock('./../../src/js/integrations/debugsymbolicatorutils', () => ({
4444
getDevServer: jest.fn(),
4545
}));
46-
jest.useFakeTimers({ advanceTimers: true });
46+
jest.useFakeTimers({
47+
advanceTimers: true,
48+
doNotFake: ['performance'], // Keep real performance API
49+
});
4750

4851
class MockNavigationContainer {
4952
currentRoute: NavigationRoute | undefined = dummyRoute;

0 commit comments

Comments
 (0)