Skip to content

Commit 0c5bfa2

Browse files
committed
mock setTimeout and update tests to make sure use fake timers is imported as early as possible
1 parent 9658566 commit 0c5bfa2

File tree

7 files changed

+42
-22
lines changed

7 files changed

+42
-22
lines changed

packages/replay-internal/test/integration/coreHandlers/handleAfterSendEvent.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { vi } from 'vitest';
22
import type { MockInstance } from 'vitest';
3+
import { useFakeTimers } from '../../utils/use-fake-timers';
4+
5+
useFakeTimers();
36

47
import { getClient } from '@sentry/core';
58
import type { ErrorEvent, Event } from '@sentry/types';
@@ -10,9 +13,7 @@ import type { ReplayContainer } from '../../../src/replay';
1013
import { Error } from '../../fixtures/error';
1114
import { Transaction } from '../../fixtures/transaction';
1215
import { resetSdkMock } from '../../mocks/resetSdkMock';
13-
import { useFakeTimers } from '../../utils/use-fake-timers';
1416

15-
useFakeTimers();
1617
let replay: ReplayContainer;
1718

1819
describe('Integration | coreHandlers | handleAfterSendEvent', () => {

packages/replay-internal/test/integration/flush.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { vi } from 'vitest';
22
import type { MockedFunction } from 'vitest';
33

4+
import { useFakeTimers } from '../utils/use-fake-timers';
5+
6+
useFakeTimers();
7+
48
import * as SentryBrowserUtils from '@sentry-internal/browser-utils';
59
import * as SentryUtils from '@sentry/utils';
610

@@ -14,9 +18,6 @@ import * as SendReplay from '../../src/util/sendReplay';
1418
import { BASE_TIMESTAMP, mockRrweb, mockSdk } from '../index';
1519
import type { DomHandler } from '../types';
1620
import { getTestEventCheckout } from '../utils/getTestEvent';
17-
import { useFakeTimers } from '../utils/use-fake-timers';
18-
19-
useFakeTimers();
2021

2122
type MockSendReplay = MockedFunction<any>;
2223
type MockAddPerformanceEntries = MockedFunction<ReplayContainer['_addPerformanceEntries']>;

packages/replay-internal/test/unit/coreHandlers/handleClick.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import { useFakeTimers } from '../../utils/use-fake-timers';
2+
3+
useFakeTimers();
4+
15
import type { Breadcrumb } from '@sentry/types';
26

37
import { BASE_TIMESTAMP } from '../..';
48
import { ClickDetector, ignoreElement } from '../../../src/coreHandlers/handleClick';
59
import type { ReplayContainer } from '../../../src/types';
6-
import { useFakeTimers } from '../../utils/use-fake-timers';
7-
8-
useFakeTimers();
9-
1010
describe('Unit | coreHandlers | handleClick', () => {
1111
describe('ClickDetector', () => {
1212
beforeEach(() => {

packages/replay-internal/test/unit/coreHandlers/util/fetchUtils.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import { useFakeTimers } from '../../../utils/use-fake-timers';
2+
3+
useFakeTimers();
4+
5+
import { vi } from 'vitest';
6+
17
import { _getResponseInfo } from '../../../../src/coreHandlers/util/fetchUtils';
28

39
describe('Unit | coreHandlers | util | fetchUtils', () => {
@@ -112,15 +118,18 @@ describe('Unit | coreHandlers | util | fetchUtils', () => {
112118
text: () => new Promise(resolve => setTimeout(() => resolve('text body'), 1000)),
113119
} as unknown as Response;
114120

115-
const res = await _getResponseInfo(
116-
true,
117-
{
118-
networkCaptureBodies: true,
119-
networkResponseHeaders: [],
120-
},
121-
response,
122-
undefined,
123-
);
121+
const [res] = await Promise.all([
122+
_getResponseInfo(
123+
true,
124+
{
125+
networkCaptureBodies: true,
126+
networkResponseHeaders: [],
127+
},
128+
response,
129+
undefined,
130+
),
131+
vi.runAllTimersAsync(),
132+
]);
124133

125134
expect(res).toEqual({
126135
_meta: { warnings: ['BODY_PARSE_ERROR'] },

packages/replay-internal/test/unit/coreHandlers/util/networkUtils.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { vi } from 'vitest';
2-
31
import { NETWORK_BODY_MAX_SIZE } from '../../../../src/constants';
42
import {
53
buildNetworkRequestOrResponse,

packages/replay-internal/test/unit/util/debounce.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { debounce } from '../../../src/util/debounce';
21
import { useFakeTimers } from '../../utils/use-fake-timers';
32

3+
useFakeTimers();
4+
5+
import { vi } from 'vitest';
6+
7+
import { debounce } from '../../../src/util/debounce';
8+
49
describe('Unit | util | debounce', () => {
5-
useFakeTimers();
610
it('delay the execution of the passed callback function by the passed minDelay', () => {
711
const callback = vi.fn();
812
const debouncedCallback = debounce(callback, 100);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { vi } from 'vitest';
22

3+
vi.mock('@sentry-internal/browser-utils', async () => ({
4+
...(await vi.importActual('@sentry-internal/browser-utils')),
5+
setTimeout: (...args) => {
6+
return setTimeout.call(global, ...args);
7+
},
8+
}));
9+
310
export function useFakeTimers(): void {
411
vi.useFakeTimers();
512
}

0 commit comments

Comments
 (0)