Skip to content

Commit 277264e

Browse files
authored
ref(replay): Remove clearSession from replay API (#6704)
As this was only used for tests, we instead make it a test util.
1 parent 95ba49a commit 277264e

File tree

8 files changed

+21
-40
lines changed

8 files changed

+21
-40
lines changed

packages/replay/src/replay.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { handleXhrSpanListener } from './coreHandlers/handleXhr';
2020
import { setupPerformanceObserver } from './coreHandlers/performanceObserver';
2121
import { createPerformanceEntries } from './createPerformanceEntry';
2222
import { createEventBuffer } from './eventBuffer';
23-
import { deleteSession } from './session/deleteSession';
2423
import { getSession } from './session/getSession';
2524
import { saveSession } from './session/saveSession';
2625
import type {
@@ -290,16 +289,6 @@ export class ReplayContainer implements ReplayContainerInterface {
290289
}
291290
}
292291

293-
/** for tests only */
294-
clearSession(): void {
295-
try {
296-
deleteSession();
297-
this.session = undefined;
298-
} catch (err) {
299-
this.handleException(err);
300-
}
301-
}
302-
303292
/**
304293
* Loads a session from storage, or creates a new one if it does not exist or
305294
* is expired.

packages/replay/test/unit/flush.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as SentryUtils from '@sentry/utils';
33
import { DEFAULT_FLUSH_MIN_DELAY, SESSION_IDLE_DURATION, WINDOW } from '../../src/constants';
44
import * as AddMemoryEntry from '../../src/util/addMemoryEntry';
55
import { createPerformanceSpans } from '../../src/util/createPerformanceSpans';
6+
import { clearSession } from '../utils/clearSession';
67
import { createPerformanceEntries } from './../../src/createPerformanceEntry';
78
import { ReplayContainer } from './../../src/replay';
89
import { useFakeTimers } from './../../test/utils/use-fake-timers';
@@ -89,7 +90,7 @@ afterEach(async () => {
8990
await new Promise(process.nextTick);
9091
jest.setSystemTime(new Date(BASE_TIMESTAMP));
9192
sessionStorage.clear();
92-
replay.clearSession();
93+
clearSession(replay);
9394
replay.loadSession({ expiry: SESSION_IDLE_DURATION });
9495
mockRecord.takeFullSnapshot.mockClear();
9596
Object.defineProperty(WINDOW, 'location', {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { captureException } from '@sentry/core';
22

33
import { DEFAULT_FLUSH_MIN_DELAY, REPLAY_SESSION_KEY, VISIBILITY_CHANGE_TIMEOUT, WINDOW } from '../../src/constants';
44
import { addEvent } from '../../src/util/addEvent';
5+
import { clearSession } from '../utils/clearSession';
56
import { ReplayContainer } from './../../src/replay';
67
import { PerformanceEntryResource } from './../fixtures/performanceEntry/resource';
78
import { BASE_TIMESTAMP, RecordMock } from './../index';
@@ -34,7 +35,7 @@ describe('Replay (errorSampleRate)', () => {
3435
});
3536

3637
afterEach(async () => {
37-
replay.clearSession();
38+
clearSession(replay);
3839
replay.stop();
3940
});
4041

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as SentryUtils from '@sentry/utils';
44

55
import { DEFAULT_FLUSH_MIN_DELAY, SESSION_IDLE_DURATION, VISIBILITY_CHANGE_TIMEOUT } from '../../src/constants';
66
import { addEvent } from '../../src/util/addEvent';
7+
import { clearSession } from '../utils/clearSession';
78
import { ReplayContainer } from './../../src/replay';
89
import { BASE_TIMESTAMP, mockRrweb, mockSdk } from './../index';
910
import { useFakeTimers } from './../utils/use-fake-timers';
@@ -50,7 +51,7 @@ describe('Replay (no sticky)', () => {
5051
jest.runAllTimers();
5152
await new Promise(process.nextTick);
5253
jest.setSystemTime(new Date(BASE_TIMESTAMP));
53-
replay.clearSession();
54+
clearSession(replay);
5455
replay.loadSession({ expiry: SESSION_IDLE_DURATION });
5556
});
5657

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ReplayContainer } from '../../src/replay';
1414
import type { RecordingEvent } from '../../src/types';
1515
import { addEvent } from '../../src/util/addEvent';
1616
import { createPerformanceSpans } from '../../src/util/createPerformanceSpans';
17+
import { clearSession } from '../utils/clearSession';
1718
import { useFakeTimers } from '../utils/use-fake-timers';
1819
import { PerformanceEntryResource } from './../fixtures/performanceEntry/resource';
1920
import { BASE_TIMESTAMP, RecordMock } from './../index';
@@ -128,7 +129,7 @@ describe('Replay', () => {
128129

129130
// Create a new session and clear mocks because a segment (from initial
130131
// checkout) will have already been uploaded by the time the tests run
131-
replay.clearSession();
132+
clearSession(replay);
132133
replay.loadSession({ expiry: 0 });
133134
mockTransportSend.mockClear();
134135
mockSendReplayRequest = replay.sendReplayRequest as MockSendReplayRequest;
@@ -142,7 +143,7 @@ describe('Replay', () => {
142143
value: prevLocation,
143144
writable: true,
144145
});
145-
replay.clearSession();
146+
clearSession(replay);
146147
jest.clearAllMocks();
147148
mockSendReplayRequest.mockRestore();
148149
mockRecord.takeFullSnapshot.mockClear();
@@ -159,7 +160,7 @@ describe('Replay', () => {
159160
});
160161

161162
it('clears session', () => {
162-
replay.clearSession();
163+
clearSession(replay);
163164
expect(WINDOW.sessionStorage.getItem(REPLAY_SESSION_KEY)).toBe(null);
164165
expect(replay.session).toBe(undefined);
165166
});
@@ -750,7 +751,7 @@ describe('Replay', () => {
750751
});
751752

752753
it('has correct timestamps when there events earlier than initial timestamp', async function () {
753-
replay.clearSession();
754+
clearSession(replay);
754755
replay.loadSession({ expiry: 0 });
755756
mockTransportSend.mockClear();
756757
Object.defineProperty(document, 'visibilityState', {

packages/replay/test/unit/session/deleteSession.test.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/replay/test/unit/stop.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as SentryUtils from '@sentry/utils';
33
import { SESSION_IDLE_DURATION, WINDOW } from '../../src/constants';
44
import { ReplayContainer } from '../../src/replay';
55
import { addEvent } from '../../src/util/addEvent';
6+
import { clearSession } from '../utils/clearSession';
67
import { Replay } from './../../src';
78
// mock functions need to be imported first
89
import { BASE_TIMESTAMP, mockRrweb, mockSdk } from './../index';
@@ -42,7 +43,7 @@ describe('Replay - stop', () => {
4243
await new Promise(process.nextTick);
4344
jest.setSystemTime(new Date(BASE_TIMESTAMP));
4445
sessionStorage.clear();
45-
replay.clearSession();
46+
clearSession(replay);
4647
replay.loadSession({ expiry: SESSION_IDLE_DURATION });
4748
mockRecord.takeFullSnapshot.mockClear();
4849
mockAddInstrumentationHandler.mockClear();

packages/replay/src/session/deleteSession.ts renamed to packages/replay/test/utils/clearSession.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { REPLAY_SESSION_KEY, WINDOW } from '../constants';
1+
import { REPLAY_SESSION_KEY, WINDOW } from '../../src/constants';
2+
import { ReplayContainer } from '../../src/types';
3+
4+
export function clearSession(replay: ReplayContainer) {
5+
deleteSession();
6+
replay.session = undefined;
7+
}
28

39
/**
410
* Deletes a session from storage
511
*/
6-
export function deleteSession(): void {
12+
function deleteSession(): void {
713
const hasSessionStorage = 'sessionStorage' in WINDOW;
814

915
if (!hasSessionStorage) {

0 commit comments

Comments
 (0)