-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy patheventProcessors.test.ts
More file actions
83 lines (62 loc) · 2.28 KB
/
eventProcessors.test.ts
File metadata and controls
83 lines (62 loc) · 2.28 KB
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
/**
* @vitest-environment jsdom
*/
import '../utils/mock-internal-setTimeout';
import type { Event } from '@sentry/core';
import { getClient, getCurrentScope } from '@sentry/core';
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { BASE_TIMESTAMP } from '..';
import { resetSdkMock } from '../mocks/resetSdkMock';
import { getTestEventIncremental } from '../utils/getTestEvent';
describe('Integration | eventProcessors', () => {
beforeAll(() => {
vi.useFakeTimers();
});
beforeEach(() => {
getCurrentScope().clear();
getCurrentScope().setClient(undefined);
});
afterEach(() => {
vi.resetAllMocks();
});
it('handles event processors properly', async () => {
const MUTATED_TIMESTAMP = BASE_TIMESTAMP + 3000;
const { mockRecord } = await resetSdkMock({
replayOptions: {
stickySession: false,
},
});
const client = getClient()!;
await vi.runAllTimersAsync();
const mockTransportSend = vi.spyOn(client.getTransport()!, 'send');
mockTransportSend.mockReset();
const handler1 = vi.fn((event: Event): Event | null => {
event.timestamp = MUTATED_TIMESTAMP;
return event;
});
const handler2 = vi.fn((): Event | null => {
return null;
});
getCurrentScope().addEventProcessor(handler1);
const TEST_EVENT = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });
mockRecord._emitter(TEST_EVENT);
vi.runAllTimers();
await new Promise(process.nextTick);
expect(mockTransportSend).toHaveBeenCalledTimes(1);
getCurrentScope().addEventProcessor(handler2);
const TEST_EVENT2 = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });
mockRecord._emitter(TEST_EVENT2);
vi.runAllTimers();
await new Promise(process.nextTick);
expect(mockTransportSend).toHaveBeenCalledTimes(1);
expect(handler1).toHaveBeenCalledTimes(2);
expect(handler2).toHaveBeenCalledTimes(1);
// This receives an envelope, which is a deeply nested array
// We only care about the fact that the timestamp was mutated
expect(mockTransportSend).toHaveBeenCalledWith(
expect.arrayContaining([
expect.arrayContaining([expect.arrayContaining([expect.objectContaining({ timestamp: MUTATED_TIMESTAMP })])]),
]),
);
});
});