-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathinit.test.ts
112 lines (90 loc) · 4.16 KB
/
init.test.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { type Integration } from '@sentry/core';
import * as sentryNode from '@sentry/node';
import type { Mock } from 'bun:test';
import { afterEach, beforeEach, describe, it, spyOn, mock, expect } from 'bun:test';
import { getClient, init } from '../src';
const PUBLIC_DSN = 'https://username@domain/123';
class MockIntegration implements Integration {
public name: string;
public setupOnce: Mock<() => void>;
public constructor(name: string) {
this.name = name;
this.setupOnce = mock(() => undefined);
}
}
describe('init()', () => {
let mockAutoPerformanceIntegrations: Mock<() => Integration[]>;
beforeEach(() => {
// @ts-expect-error weird
mockAutoPerformanceIntegrations = spyOn(sentryNode, 'getAutoPerformanceIntegrations');
});
afterEach(() => {
mockAutoPerformanceIntegrations.mockRestore();
});
describe('integrations', () => {
it("doesn't install default integrations if told not to", () => {
init({ dsn: PUBLIC_DSN, defaultIntegrations: false });
const client = getClient();
expect(client?.getOptions().integrations).toEqual([]);
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
});
it('installs merged default integrations, with overrides provided through options', () => {
const mockDefaultIntegrations = [
new MockIntegration('Some mock integration 2.1'),
new MockIntegration('Some mock integration 2.2'),
];
const mockIntegrations = [
new MockIntegration('Some mock integration 2.1'),
new MockIntegration('Some mock integration 2.3'),
];
init({ dsn: PUBLIC_DSN, integrations: mockIntegrations, defaultIntegrations: mockDefaultIntegrations });
expect(mockDefaultIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(0);
expect(mockDefaultIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
});
it('installs integrations returned from a callback function', () => {
const mockDefaultIntegrations = [
new MockIntegration('Some mock integration 3.1'),
new MockIntegration('Some mock integration 3.2'),
];
const newIntegration = new MockIntegration('Some mock integration 3.3');
init({
dsn: PUBLIC_DSN,
defaultIntegrations: mockDefaultIntegrations,
integrations: integrations => {
const newIntegrations = [...integrations];
newIntegrations[1] = newIntegration;
return newIntegrations;
},
});
expect(mockDefaultIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockDefaultIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(0);
expect(newIntegration.setupOnce).toHaveBeenCalledTimes(1);
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(0);
});
it('installs performance default instrumentations if tracing is enabled', () => {
const autoPerformanceIntegrations = [new MockIntegration('Performance integration')];
mockAutoPerformanceIntegrations.mockImplementation(() => autoPerformanceIntegrations);
const mockIntegrations = [
new MockIntegration('Some mock integration 4.1'),
new MockIntegration('Some mock integration 4.3'),
];
init({
dsn: PUBLIC_DSN,
integrations: mockIntegrations,
tracesSampleRate: 1,
});
expect(mockIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockIntegrations[1]?.setupOnce).toHaveBeenCalledTimes(1);
expect(autoPerformanceIntegrations[0]?.setupOnce).toHaveBeenCalledTimes(1);
expect(mockAutoPerformanceIntegrations).toHaveBeenCalledTimes(1);
const integrations = getClient()?.getOptions().integrations;
expect(integrations).toBeArray();
expect(integrations?.map(({ name }) => name)).toContain('Performance integration');
expect(integrations?.map(({ name }) => name)).toContain('Some mock integration 4.1');
expect(integrations?.map(({ name }) => name)).toContain('Some mock integration 4.3');
});
});
});