-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathsdk.test.ts
More file actions
161 lines (125 loc) · 4.92 KB
/
sdk.test.ts
File metadata and controls
161 lines (125 loc) · 4.92 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { afterEach, describe, expect, it, vi } from 'vitest';
import * as Sentry from '../../src/light';
import { LightNodeClient } from '../../src/light/client';
import { cleanupLightSdk, mockLightSdkInit, resetGlobals } from '../helpers/mockLightSdkInit';
describe('Light Mode | SDK', () => {
afterEach(() => {
cleanupLightSdk();
});
describe('init', () => {
it('returns a LightNodeClient', () => {
const client = mockLightSdkInit();
expect(client).toBeInstanceOf(LightNodeClient);
});
it('sets correct SDK metadata', () => {
const client = mockLightSdkInit();
const metadata = client?.getOptions()._metadata;
expect(metadata?.sdk?.name).toBe('sentry.javascript.node-light');
expect(metadata?.sdk?.packages).toEqual([
{
name: 'npm:@sentry/node-core',
version: expect.any(String),
},
]);
});
it('sets the client on the current scope', () => {
const client = mockLightSdkInit();
expect(Sentry.getClient()).toBe(client);
});
it('applies initialScope options', () => {
mockLightSdkInit({
initialScope: {
tags: { initialTag: 'initialValue' },
user: { id: 'test-user' },
},
});
const scope = Sentry.getCurrentScope();
expect(scope.getScopeData().tags).toEqual({ initialTag: 'initialValue' });
expect(scope.getScopeData().user).toEqual({ id: 'test-user' });
});
it('respects environment from options', () => {
const client = mockLightSdkInit({
environment: 'test-environment',
});
expect(client?.getOptions().environment).toBe('test-environment');
});
it('respects release from options', () => {
const client = mockLightSdkInit({
release: 'test-release@1.0.0',
});
expect(client?.getOptions().release).toBe('test-release@1.0.0');
});
});
describe('initWithoutDefaultIntegrations', () => {
it('initializes without default integrations', () => {
resetGlobals();
const client = Sentry.initWithoutDefaultIntegrations({
dsn: 'https://username@domain/123',
});
// Should have no integrations
const integrations = client.getOptions().integrations;
expect(integrations).toEqual([]);
});
});
describe('getDefaultIntegrations', () => {
it('returns an array of integrations', () => {
const integrations = Sentry.getDefaultIntegrations();
expect(Array.isArray(integrations)).toBe(true);
expect(integrations.length).toBeGreaterThan(0);
// Check that some expected integrations are present
const integrationNames = integrations.map(i => i.name);
expect(integrationNames).toContain('EventFilters');
expect(integrationNames).toContain('FunctionToString');
expect(integrationNames).toContain('LinkedErrors');
expect(integrationNames).toContain('OnUncaughtException');
expect(integrationNames).toContain('OnUnhandledRejection');
});
it('includes Http integration for request isolation and outgoing trace propagation', () => {
const integrations = Sentry.getDefaultIntegrations();
const integrationNames = integrations.map(i => i.name);
expect(integrationNames).toContain('Http');
});
it('includes NodeFetch integration for outgoing fetch trace propagation', () => {
const integrations = Sentry.getDefaultIntegrations();
const integrationNames = integrations.map(i => i.name);
expect(integrationNames).toContain('NodeFetch');
});
it('includes spanStreaming integration when traceLifecycle is "stream"', () => {
const integrations = Sentry.getDefaultIntegrations({ traceLifecycle: 'stream' });
const integrationNames = integrations.map(i => i.name);
expect(integrationNames).toContain('SpanStreaming');
});
it("doesn't include spanStreaming integration when traceLifecycle is not 'stream'", () => {
const integrations = Sentry.getDefaultIntegrations();
const integrationNames = integrations.map(i => i.name);
expect(integrationNames).not.toContain('SpanStreaming');
});
});
describe('isInitialized', () => {
it('returns false before init', () => {
resetGlobals();
expect(Sentry.isInitialized()).toBe(false);
});
it('returns true after init', () => {
mockLightSdkInit();
expect(Sentry.isInitialized()).toBe(true);
});
});
describe('close', () => {
it('flushes and closes the client', async () => {
const client = mockLightSdkInit();
const flushSpy = vi.spyOn(client!, 'flush');
await Sentry.close();
expect(flushSpy).toHaveBeenCalled();
});
});
describe('flush', () => {
it('flushes pending events', async () => {
const beforeSend = vi.fn(() => null);
mockLightSdkInit({ beforeSend });
Sentry.captureException(new Error('test'));
await Sentry.flush();
expect(beforeSend).toHaveBeenCalledTimes(1);
});
});
});