|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import '../utils/mock-internal-setTimeout'; |
| 6 | +import type { ReplayEndEvent, ReplayStartEvent } from '@sentry/core'; |
| 7 | +import { getClient } from '@sentry/core'; |
| 8 | +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 9 | +import type { Replay } from '../../src/integration'; |
| 10 | +import type { ReplayContainer } from '../../src/replay'; |
| 11 | +import { BASE_TIMESTAMP } from '../index'; |
| 12 | +import { resetSdkMock } from '../mocks/resetSdkMock'; |
| 13 | + |
| 14 | +describe('Integration | lifecycle hooks', () => { |
| 15 | + let replay: ReplayContainer; |
| 16 | + let integration: Replay; |
| 17 | + let startEvents: ReplayStartEvent[]; |
| 18 | + let endEvents: ReplayEndEvent[]; |
| 19 | + let unsubscribes: Array<() => void>; |
| 20 | + |
| 21 | + beforeAll(() => { |
| 22 | + vi.useFakeTimers(); |
| 23 | + }); |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + ({ replay, integration } = await resetSdkMock({ |
| 27 | + replayOptions: { stickySession: false }, |
| 28 | + sentryOptions: { replaysSessionSampleRate: 0.0 }, |
| 29 | + autoStart: false, |
| 30 | + })); |
| 31 | + |
| 32 | + startEvents = []; |
| 33 | + endEvents = []; |
| 34 | + const client = getClient()!; |
| 35 | + unsubscribes = [ |
| 36 | + client.on('replayStart', event => startEvents.push(event)), |
| 37 | + client.on('replayEnd', event => endEvents.push(event)), |
| 38 | + ]; |
| 39 | + |
| 40 | + await vi.runAllTimersAsync(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(async () => { |
| 44 | + unsubscribes.forEach(off => off()); |
| 45 | + await integration.stop(); |
| 46 | + await vi.runAllTimersAsync(); |
| 47 | + vi.setSystemTime(new Date(BASE_TIMESTAMP)); |
| 48 | + }); |
| 49 | + |
| 50 | + it('fires replayStart with session mode when start() is called', () => { |
| 51 | + integration.start(); |
| 52 | + |
| 53 | + expect(startEvents).toHaveLength(1); |
| 54 | + expect(startEvents[0]).toEqual({ |
| 55 | + sessionId: expect.any(String), |
| 56 | + recordingMode: 'session', |
| 57 | + }); |
| 58 | + expect(startEvents[0]!.sessionId).toBe(replay.session!.id); |
| 59 | + }); |
| 60 | + |
| 61 | + it('fires replayStart with buffer mode when startBuffering() is called', () => { |
| 62 | + integration.startBuffering(); |
| 63 | + |
| 64 | + expect(startEvents).toHaveLength(1); |
| 65 | + expect(startEvents[0]).toEqual({ |
| 66 | + sessionId: expect.any(String), |
| 67 | + recordingMode: 'buffer', |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('fires replayEnd with reason "manual" when integration.stop() is called', async () => { |
| 72 | + integration.start(); |
| 73 | + const sessionId = replay.session!.id; |
| 74 | + |
| 75 | + await integration.stop(); |
| 76 | + |
| 77 | + expect(endEvents).toHaveLength(1); |
| 78 | + expect(endEvents[0]).toEqual({ sessionId, reason: 'manual' }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('forwards the internal stop reason to replayEnd subscribers', async () => { |
| 82 | + integration.start(); |
| 83 | + const sessionId = replay.session!.id; |
| 84 | + |
| 85 | + await replay.stop({ reason: 'mutationLimit' }); |
| 86 | + |
| 87 | + expect(endEvents).toHaveLength(1); |
| 88 | + expect(endEvents[0]).toEqual({ sessionId, reason: 'mutationLimit' }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not fire replayEnd twice when stop() is called while already stopped', async () => { |
| 92 | + integration.start(); |
| 93 | + |
| 94 | + await replay.stop({ reason: 'sendError' }); |
| 95 | + await replay.stop({ reason: 'sendError' }); |
| 96 | + |
| 97 | + expect(endEvents).toHaveLength(1); |
| 98 | + expect(endEvents[0]!.reason).toBe('sendError'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('stops invoking callbacks after the returned unsubscribe is called', () => { |
| 102 | + const [offStart] = unsubscribes; |
| 103 | + offStart!(); |
| 104 | + |
| 105 | + integration.start(); |
| 106 | + |
| 107 | + expect(startEvents).toHaveLength(0); |
| 108 | + }); |
| 109 | +}); |
0 commit comments