Skip to content

Commit f0c9458

Browse files
authored
test(replay): Rewrite custom matcher to be recognized by VSCode (#16141)
I was always annoyed that this was not properly highlighted by VSCode, so I looked into this a bit, and actually refactored it a bit to be more type-safe in the vitest world we now live in :) I got rid of `toHaveSentReplay` in favor of simply always using `toHaveLastSentReplay` (which we already used almost everywhere). I also moved the type definition into a separate file and ensured it actually works, at leas tin my vscode/cursor instance. This also kind of surfaced some type errors that existed in tests, but have not been shown (??) for whatever reason... no idea what is going on there.
1 parent 8dd90ae commit f0c9458

File tree

6 files changed

+194
-287
lines changed

6 files changed

+194
-287
lines changed

packages/replay-internal/test.setup.ts

-254
This file was deleted.

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

+14-31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import '../utils/mock-internal-setTimeout';
66
import { captureException, getClient } from '@sentry/core';
7+
import type { MockInstance } from 'vitest';
78
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
89
import {
910
BUFFER_CHECKOUT_TIME,
@@ -34,15 +35,15 @@ async function waitForFlush() {
3435
}
3536

3637
describe('Integration | errorSampleRate', () => {
38+
beforeAll(() => {
39+
vi.useFakeTimers();
40+
});
41+
3742
describe('basic', () => {
3843
let replay: ReplayContainer;
3944
let mockRecord: RecordMock;
4045
let domHandler: DomHandler;
4146

42-
beforeAll(() => {
43-
vi.useFakeTimers();
44-
});
45-
4647
beforeEach(async () => {
4748
({ mockRecord, domHandler, replay } = await resetSdkMock({
4849
replayOptions: {
@@ -163,14 +164,14 @@ describe('Integration | errorSampleRate', () => {
163164
const ADVANCED_TIME = 86400000;
164165
const optionsEvent = createOptionsEvent(replay);
165166

166-
expect(replay.session.started).toBe(BASE_TIMESTAMP);
167+
expect(replay.session?.started).toBe(BASE_TIMESTAMP);
167168

168169
// advance time to make sure replay duration is invalid
169170
vi.advanceTimersByTime(ADVANCED_TIME);
170171

171172
// full snapshot should update session start time
172173
mockRecord.takeFullSnapshot(true);
173-
expect(replay.session.started).toBe(BASE_TIMESTAMP + ADVANCED_TIME);
174+
expect(replay.session?.started).toBe(BASE_TIMESTAMP + ADVANCED_TIME);
174175
expect(replay.recordingMode).toBe('buffer');
175176

176177
// advance so we can flush
@@ -253,7 +254,7 @@ describe('Integration | errorSampleRate', () => {
253254

254255
await vi.advanceTimersToNextTimerAsync();
255256

256-
expect(replay).toHaveSentReplay({
257+
expect(replay).toHaveLastSentReplay({
257258
recordingPayloadHeader: { segment_id: 0 },
258259
replayEventPayload: expect.objectContaining({
259260
replay_type: 'buffer',
@@ -339,7 +340,7 @@ describe('Integration | errorSampleRate', () => {
339340

340341
await vi.advanceTimersToNextTimerAsync();
341342

342-
expect(replay).toHaveSentReplay({
343+
expect(replay).toHaveLastSentReplay({
343344
recordingPayloadHeader: { segment_id: 0 },
344345
replayEventPayload: expect.objectContaining({
345346
replay_type: 'buffer',
@@ -364,24 +365,6 @@ describe('Integration | errorSampleRate', () => {
364365
},
365366
]),
366367
});
367-
368-
vi.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
369-
// Check that click will not get captured
370-
domHandler({
371-
name: 'click',
372-
event: new Event('click'),
373-
});
374-
375-
await waitForFlush();
376-
377-
// This is still the last replay sent since we passed `continueRecording:
378-
// false`.
379-
expect(replay).toHaveLastSentReplay({
380-
recordingPayloadHeader: { segment_id: 1 },
381-
replayEventPayload: expect.objectContaining({
382-
replay_type: 'buffer',
383-
}),
384-
});
385368
});
386369

387370
// This tests a regression where we were calling flush indiscriminantly in `stop()`
@@ -672,7 +655,7 @@ describe('Integration | errorSampleRate', () => {
672655
expect(replay.session?.id).toBe(oldSessionId);
673656

674657
// buffered events
675-
expect(replay).toHaveSentReplay({
658+
expect(replay).toHaveLastSentReplay({
676659
recordingPayloadHeader: { segment_id: 0 },
677660
replayEventPayload: expect.objectContaining({
678661
replay_type: 'buffer',
@@ -709,7 +692,7 @@ describe('Integration | errorSampleRate', () => {
709692
vi.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
710693
await vi.advanceTimersToNextTimerAsync();
711694

712-
expect(replay).toHaveSentReplay({
695+
expect(replay).toHaveLastSentReplay({
713696
recordingData: JSON.stringify([
714697
{ data: { isCheckout: true }, timestamp: BASE_TIMESTAMP, type: 2 },
715698
optionsEvent,
@@ -762,7 +745,7 @@ describe('Integration | errorSampleRate', () => {
762745
expect(replay.session?.started).toBe(BASE_TIMESTAMP + ELAPSED);
763746

764747
// Does not capture mouse click
765-
expect(replay).toHaveSentReplay({
748+
expect(replay).toHaveLastSentReplay({
766749
recordingPayloadHeader: { segment_id: 0 },
767750
replayEventPayload: expect.objectContaining({
768751
// Make sure the old performance event is thrown out
@@ -846,7 +829,7 @@ describe('Integration | errorSampleRate', () => {
846829
await waitForFlush();
847830

848831
expect(replay.session?.id).toBe(sessionId);
849-
expect(replay).toHaveSentReplay({
832+
expect(replay).toHaveLastSentReplay({
850833
recordingPayloadHeader: { segment_id: 0 },
851834
});
852835

@@ -1047,7 +1030,7 @@ describe('Integration | errorSampleRate', () => {
10471030
await vi.advanceTimersToNextTimerAsync();
10481031

10491032
// Buffered events before error
1050-
expect(replay).toHaveSentReplay({
1033+
expect(replay).toHaveLastSentReplay({
10511034
recordingPayloadHeader: { segment_id: 0 },
10521035
recordingData: JSON.stringify([
10531036
{ data: { isCheckout: true }, timestamp: BASE_TIMESTAMP, type: 2 },

0 commit comments

Comments
 (0)