-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathmockSdk.ts
97 lines (83 loc) · 2.46 KB
/
mockSdk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import type { Envelope, Transport, TransportMakeRequestResponse } from '@sentry/core';
import { vi } from 'vitest';
import type { Replay as ReplayIntegration } from '../../src/integration';
import type { ReplayContainer } from '../../src/replay';
import type { ReplayConfiguration } from '../../src/types';
import type { TestClientOptions } from '../utils/TestClient';
import { getDefaultClientOptions, init } from '../utils/TestClient';
export interface MockSdkParams {
replayOptions?: ReplayConfiguration;
sentryOptions?: Partial<TestClientOptions>;
autoStart?: boolean;
}
class MockTransport implements Transport {
send: (request: Envelope) => PromiseLike<TransportMakeRequestResponse>;
constructor() {
this.send = vi.fn(async () => {
return {
statusCode: 200,
};
});
}
async flush() {
return true;
}
async sendEvent(_e: Event) {
return {
status: 'skipped',
event: 'ok',
type: 'transaction',
};
}
async sendSession() {
return;
}
async recordLostEvent() {
return;
}
async close() {
return;
}
}
export async function mockSdk({ replayOptions, sentryOptions, autoStart = true }: MockSdkParams = {}): Promise<{
replay: ReplayContainer;
integration: ReplayIntegration;
}> {
const { Replay } = await import('../../src/integration');
// Scope this to the test, instead of the module
let _initialized = false;
class TestReplayIntegration extends Replay {
protected get _isInitialized(): boolean {
return _initialized;
}
protected set _isInitialized(value: boolean) {
_initialized = value;
}
public afterAllSetup(): void {
// do nothing, we need to manually initialize this
}
}
const replayIntegration = new TestReplayIntegration({
stickySession: false,
minReplayDuration: 0,
...replayOptions,
});
const client = init({
...getDefaultClientOptions(),
dsn: 'https://[email protected]/1',
sendClientReports: false,
transport: () => new MockTransport(),
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 0.0,
...sentryOptions,
integrations: [replayIntegration],
})!;
// Instead of `afterAllSetup`, which is tricky to test, we call this manually here
replayIntegration['_setup'](client);
if (autoStart) {
// Only exists in our mock
replayIntegration['_initialize'](client);
}
const replay = replayIntegration['_replay']!;
return { replay, integration: replayIntegration };
}