Skip to content

test(replay): Improve replay test mocks #6313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions packages/replay/__mocks__/@sentry/browser/index.ts

This file was deleted.

24 changes: 12 additions & 12 deletions packages/replay/test/mocks/mockRrweb.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { record as rrwebRecord } from 'rrweb';

import { RecordingEvent } from '../../src/types';

type RecordAdditionalProperties = {
Expand All @@ -12,7 +14,7 @@ type RecordAdditionalProperties = {
_emitter: (event: RecordingEvent, ...args: any[]) => void;
};

export type RecordMock = jest.MockedFunction<typeof rrweb.record> & RecordAdditionalProperties;
export type RecordMock = jest.MockedFunction<typeof rrwebRecord> & RecordAdditionalProperties;

function createCheckoutPayload(isCheckout: boolean = true) {
return {
Expand All @@ -22,8 +24,7 @@ function createCheckoutPayload(isCheckout: boolean = true) {
};
}

jest.mock('rrweb', () => {
const ActualRrweb = jest.requireActual('rrweb');
export function mockRrweb(): { record: RecordMock } {
const mockRecordFn: jest.Mock & Partial<RecordAdditionalProperties> = jest.fn(({ emit }) => {
mockRecordFn._emitter = emit;

Expand All @@ -40,17 +41,16 @@ jest.mock('rrweb', () => {
mockRecordFn._emitter(createCheckoutPayload(isCheckout), isCheckout);
});

return {
...ActualRrweb,
record: mockRecordFn,
};
});
jest.mock('rrweb', () => {
const ActualRrweb = jest.requireActual('rrweb');

// XXX: Intended to be after `mock('rrweb')`
import * as rrweb from 'rrweb';
return {
...ActualRrweb,
record: mockRecordFn,
};
});

export function mockRrweb(): { record: RecordMock } {
return {
record: rrweb.record as RecordMock,
record: mockRecordFn as RecordMock,
};
}
6 changes: 3 additions & 3 deletions packages/replay/test/mocks/mockSdk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
jest.unmock('@sentry/browser');

import { BrowserOptions, init } from '@sentry/browser';
import type { BrowserOptions } from '@sentry/browser';
import { Envelope, Transport } from '@sentry/types';

import { Replay as ReplayClass } from '../../src';
Expand Down Expand Up @@ -49,6 +47,8 @@ export async function mockSdk({
transport: () => new MockTransport(),
},
}: MockSdkParams = {}): Promise<{ replay: ReplayClass }> {
const { init } = jest.requireActual('@sentry/browser');

const { Replay } = await import('../../src');
const replay = new Replay(replayOptions);

Expand Down
11 changes: 11 additions & 0 deletions packages/replay/test/unit/session/Session.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
jest.mock('./../../../src/session/saveSession');

jest.mock('@sentry/browser', () => {
return {
getCurrentHub: jest.fn(() => {
return {
captureEvent: jest.fn(),
getClient: jest.fn(() => ({ getDsn: jest.fn() })),
};
}),
};
});

import * as Sentry from '@sentry/browser';

import { saveSession } from '../../../src/session/saveSession';
Expand Down