-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathspotlight.test.ts
191 lines (153 loc) · 5.88 KB
/
spotlight.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import * as http from 'node:http';
import { createEnvelope, logger } from '@sentry/core';
import type { Envelope, EventEnvelope } from '@sentry/core';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { spotlightIntegration } from '../../src/integrations/spotlight';
import { NodeClient } from '../../src/sdk/client';
import { getDefaultNodeClientOptions } from '../helpers/getDefaultNodeClientOptions';
vi.mock('node:http', async () => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const original = (await vi.importActual('node:http')) as typeof import('node:http');
return {
...original,
request: original.request,
};
});
describe('Spotlight', () => {
const loggerSpy = vi.spyOn(logger, 'warn');
afterEach(() => {
loggerSpy.mockClear();
vi.clearAllMocks();
});
const options = getDefaultNodeClientOptions();
const client = new NodeClient(options);
it('has a name', () => {
const integration = spotlightIntegration();
expect(integration.name).toEqual('Spotlight');
});
it('registers a callback on the `beforeEnvelope` hook', () => {
const clientWithSpy = {
...client,
on: vi.fn(),
};
const integration = spotlightIntegration();
// @ts-expect-error - this is fine in tests
integration.setup(clientWithSpy);
expect(clientWithSpy.on).toHaveBeenCalledWith('beforeEnvelope', expect.any(Function));
});
it('sends an envelope POST request to the sidecar url', () => {
const httpSpy = vi.spyOn(http, 'request').mockImplementationOnce(() => {
return {
on: vi.fn(),
write: vi.fn(),
end: vi.fn(),
} as any;
});
let callback: (envelope: Envelope) => void = () => {};
const clientWithSpy = {
...client,
on: vi.fn().mockImplementationOnce((_, cb) => (callback = cb)),
};
const integration = spotlightIntegration();
// @ts-expect-error - this is fine in tests
integration.setup(clientWithSpy);
const envelope = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
]);
callback(envelope);
expect(httpSpy).toHaveBeenCalledWith(
{
headers: {
'Content-Type': 'application/x-sentry-envelope',
},
hostname: 'localhost',
method: 'POST',
path: '/stream',
port: '8969',
},
expect.any(Function),
);
});
it('sends an envelope POST request to a custom sidecar url', () => {
const httpSpy = vi.spyOn(http, 'request').mockImplementationOnce(() => {
return {
on: vi.fn(),
write: vi.fn(),
end: vi.fn(),
} as any;
});
let callback: (envelope: Envelope) => void = () => {};
const clientWithSpy = {
...client,
on: vi.fn().mockImplementationOnce((_, cb) => (callback = cb)),
};
const integration = spotlightIntegration({ sidecarUrl: 'http://mylocalhost:8888/abcd' });
// @ts-expect-error - this is fine in tests
integration.setup(clientWithSpy);
const envelope = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
]);
callback(envelope);
expect(httpSpy).toHaveBeenCalledWith(
{
headers: {
'Content-Type': 'application/x-sentry-envelope',
},
hostname: 'mylocalhost',
method: 'POST',
path: '/abcd',
port: '8888',
},
expect.any(Function),
);
});
describe('no-ops if', () => {
it('an invalid URL is passed', () => {
const integration = spotlightIntegration({ sidecarUrl: 'invalid-url' });
integration.setup!(client);
expect(loggerSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid sidecar URL: invalid-url'));
});
});
it('warns if the NODE_ENV variable doesn\'t equal "development"', () => {
const oldEnvValue = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
const integration = spotlightIntegration({ sidecarUrl: 'http://localhost:8969' });
integration.setup!(client);
expect(loggerSpy).toHaveBeenCalledWith(
expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"),
);
process.env.NODE_ENV = oldEnvValue;
});
it('doesn\'t warn if the NODE_ENV variable equals "development"', () => {
const oldEnvValue = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
const integration = spotlightIntegration({ sidecarUrl: 'http://localhost:8969' });
integration.setup!(client);
expect(loggerSpy).not.toHaveBeenCalledWith(
expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"),
);
process.env.NODE_ENV = oldEnvValue;
});
it('handles `process` not being available', () => {
const originalProcess = process;
// @ts-expect-error - TS complains but we explicitly wanna test this
delete global.process;
const integration = spotlightIntegration({ sidecarUrl: 'http://localhost:8969' });
integration.setup!(client);
expect(loggerSpy).not.toHaveBeenCalledWith(
expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"),
);
global.process = originalProcess;
});
it('handles `process.env` not being available', () => {
const originalEnv = process.env;
// @ts-expect-error - TS complains but we explicitly wanna test this
delete process.env;
const integration = spotlightIntegration({ sidecarUrl: 'http://localhost:8969' });
integration.setup!(client);
expect(loggerSpy).not.toHaveBeenCalledWith(
expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"),
);
process.env = originalEnv;
});
});