Skip to content

Commit 5de6e43

Browse files
authored
test(replay): Streamline replay mocking (#6438)
1 parent 33fd2d6 commit 5de6e43

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

packages/replay/src/integration.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ export class Replay implements Integration {
3030

3131
readonly options: ReplayPluginOptions;
3232

33-
/** In tests, this is only called the first time */
34-
protected _hasCalledSetupOnce: boolean = false;
33+
protected get _isInitialized(): boolean {
34+
return _initialized;
35+
}
36+
37+
protected set _isInitialized(value: boolean) {
38+
_initialized = value;
39+
}
3540

3641
private _replay?: ReplayContainer;
3742

@@ -112,13 +117,13 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`,
112117
: `${this.recordingOptions.blockSelector},${MEDIA_SELECTORS}`;
113118
}
114119

115-
if (isBrowser() && _initialized) {
120+
if (isBrowser() && this._isInitialized) {
116121
const error = new Error('Multiple Sentry Session Replay instances are not supported');
117122
captureInternalException(error);
118123
throw error;
119124
}
120125

121-
_initialized = true;
126+
this._isInitialized = true;
122127
}
123128

124129
/**
@@ -137,7 +142,6 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`,
137142
}
138143

139144
this._setup();
140-
this._hasCalledSetupOnce = true;
141145

142146
// XXX: See method comments above
143147
setTimeout(() => this.start());

packages/replay/test/mocks/mockSdk.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BrowserOptions } from '@sentry/browser';
1+
import { BrowserOptions, init } from '@sentry/browser';
22
import { Envelope, Transport } from '@sentry/types';
33

44
import { Replay as ReplayIntegration } from '../../src';
@@ -39,10 +39,24 @@ export async function mockSdk({ replayOptions, sentryOptions }: MockSdkParams =
3939
replay: ReplayContainer;
4040
integration: ReplayIntegration;
4141
}> {
42-
const { init } = jest.requireActual('@sentry/browser');
43-
4442
const { Replay } = await import('../../src');
45-
const replayIntegration = new Replay({
43+
44+
// Scope this to the test, instead of the module
45+
let _initialized = false;
46+
class TestReplayIntegration extends Replay {
47+
protected get _isInitialized(): boolean {
48+
return _initialized;
49+
}
50+
protected set _isInitialized(value: boolean) {
51+
_initialized = value;
52+
}
53+
54+
public setupOnce(): void {
55+
// do nothing
56+
}
57+
}
58+
59+
const replayIntegration = new TestReplayIntegration({
4660
stickySession: false,
4761
...replayOptions,
4862
});
@@ -54,18 +68,14 @@ export async function mockSdk({ replayOptions, sentryOptions }: MockSdkParams =
5468
transport: () => new MockTransport(),
5569
replaysSessionSampleRate: 1.0,
5670
replaysOnErrorSampleRate: 0.0,
71+
defaultIntegrations: false,
5772
...sentryOptions,
5873
integrations: [replayIntegration],
5974
});
6075

61-
// setupOnce is only called the first time, so we ensure to manually run setup in subsequent calls
62-
if (replayIntegration['_hasCalledSetupOnce']) {
63-
// The first time the integration is used, `start()` is called (in setupOnce)
64-
// For consistency, we want to stop that
65-
replayIntegration.stop();
66-
} else {
67-
replayIntegration['_setup']();
68-
}
76+
// Instead of `setupOnce`, which is tricky to test, we call this manually here
77+
replayIntegration['_setup']();
78+
replayIntegration.start();
6979

7080
const replay = replayIntegration['_replay']!;
7181

packages/replay/test/mocks/index.ts renamed to packages/replay/test/mocks/resetSdkMock.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getCurrentHub } from '@sentry/core';
33
import { ReplayContainer } from '../../src/replay';
44
import { BASE_TIMESTAMP, RecordMock } from './../index';
55
import { DomHandler, MockTransportSend } from './../types';
6-
import { MockSdkParams } from './mockSdk';
6+
import { mockSdk, MockSdkParams } from './mockSdk';
77

88
export async function resetSdkMock({ replayOptions, sentryOptions }: MockSdkParams): Promise<{
99
domHandler: DomHandler;
@@ -36,7 +36,6 @@ export async function resetSdkMock({ replayOptions, sentryOptions }: MockSdkPara
3636
const SentryCore = await import('@sentry/core');
3737
const spyCaptureException = jest.spyOn(SentryCore, 'captureException');
3838

39-
const { mockSdk } = await import('./mockSdk');
4039
const { replay } = await mockSdk({
4140
replayOptions,
4241
sentryOptions,

packages/replay/test/unit/index-errorSampleRate.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { REPLAY_SESSION_KEY, VISIBILITY_CHANGE_TIMEOUT, WINDOW } from '../../src
66
import { ReplayContainer } from './../../src/replay';
77
import { PerformanceEntryResource } from './../fixtures/performanceEntry/resource';
88
import { BASE_TIMESTAMP, RecordMock } from './../index';
9-
import { resetSdkMock } from './../mocks';
9+
import { resetSdkMock } from './../mocks/resetSdkMock';
1010
import { DomHandler, MockTransportSend } from './../types';
1111
import { useFakeTimers } from './../utils/use-fake-timers';
1212

packages/replay/test/unit/index-handleGlobalEvent.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { REPLAY_EVENT_NAME } from '../../src/constants';
44
import { ReplayContainer } from './../../src/replay';
55
import { Error } from './../fixtures/error';
66
import { Transaction } from './../fixtures/transaction';
7-
import { resetSdkMock } from './../mocks';
7+
import { resetSdkMock } from './../mocks/resetSdkMock';
88
import { useFakeTimers } from './../utils/use-fake-timers';
99

1010
useFakeTimers();

packages/replay/test/unit/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { RecordingEvent } from '../../src/types';
1010
import { useFakeTimers } from '../utils/use-fake-timers';
1111
import { PerformanceEntryResource } from './../fixtures/performanceEntry/resource';
1212
import { BASE_TIMESTAMP, RecordMock } from './../index';
13-
import { resetSdkMock } from './../mocks';
13+
import { resetSdkMock } from './../mocks/resetSdkMock';
1414
import { DomHandler, MockTransportSend } from './../types';
1515

1616
useFakeTimers();

0 commit comments

Comments
 (0)