-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathserverSdk.test.ts
More file actions
285 lines (234 loc) · 9.2 KB
/
serverSdk.test.ts
File metadata and controls
285 lines (234 loc) · 9.2 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import type { Integration } from '@sentry/core';
import { GLOBAL_OBJ } from '@sentry/core';
import { getCurrentScope } from '@sentry/node';
import * as SentryNode from '@sentry/node';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { init } from '../src/server';
// normally this is set as part of the build process, so mock it here
(GLOBAL_OBJ as typeof GLOBAL_OBJ & { _sentryRewriteFramesDistDir: string })._sentryRewriteFramesDistDir = '.next';
const nodeInit = vi.spyOn(SentryNode, 'init');
function findIntegrationByName(integrations: Integration[] = [], name: string): Integration | undefined {
return integrations.find(integration => integration.name === name);
}
describe('Server init()', () => {
afterEach(() => {
vi.clearAllMocks();
SentryNode.getGlobalScope().clear();
SentryNode.getIsolationScope().clear();
SentryNode.getCurrentScope().clear();
SentryNode.getCurrentScope().setClient(undefined);
delete process.env.VERCEL;
});
it('inits the Node SDK', () => {
expect(nodeInit).toHaveBeenCalledTimes(0);
init({});
expect(nodeInit).toHaveBeenCalledTimes(1);
expect(nodeInit).toHaveBeenLastCalledWith(
expect.objectContaining({
_metadata: {
sdk: {
name: 'sentry.javascript.nextjs',
version: expect.any(String),
packages: [
{
name: 'npm:@sentry/nextjs',
version: expect.any(String),
},
{
name: 'npm:@sentry/node',
version: expect.any(String),
},
],
},
},
environment: 'test',
// Integrations are tested separately, and we can't be more specific here without depending on the order in
// which integrations appear in the array, which we can't guarantee.
//
// TODO: If we upgrade to Jest 28+, we can follow Jest's example matcher and create an
// `expect.ArrayContainingInAnyOrder`. See
// https://github.com/facebook/jest/blob/main/examples/expect-extend/toBeWithinRange.ts.
defaultIntegrations: expect.any(Array),
}),
);
});
it("doesn't reinitialize the node SDK if already initialized", () => {
expect(nodeInit).toHaveBeenCalledTimes(0);
init({});
expect(nodeInit).toHaveBeenCalledTimes(1);
init({});
expect(nodeInit).toHaveBeenCalledTimes(1);
});
// TODO: test `vercel` tag when running on Vercel
// Can't just add the test and set env variables, since the value in `index.server.ts`
// is resolved when importing.
it('does not apply `vercel` tag when not running on vercel', () => {
const currentScope = getCurrentScope();
expect(process.env.VERCEL).toBeUndefined();
init({});
// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags.vercel).toBeUndefined();
});
describe('integrations', () => {
// Options passed by `@sentry/nextjs`'s `init` to `@sentry/node`'s `init` after modifying them
type ModifiedInitOptions = { integrations: Integration[]; defaultIntegrations: Integration[] };
it('adds default integrations', () => {
init({});
const nodeInitOptions = nodeInit.mock.calls[0]?.[0] as ModifiedInitOptions;
const integrationNames = nodeInitOptions.defaultIntegrations.map(integration => integration.name);
const onUncaughtExceptionIntegration = findIntegrationByName(
nodeInitOptions.defaultIntegrations,
'OnUncaughtException',
);
expect(integrationNames).toContain('DistDirRewriteFrames');
expect(onUncaughtExceptionIntegration).toBeDefined();
});
it('supports passing unrelated integrations through options', () => {
init({ integrations: [SentryNode.consoleIntegration()] });
const nodeInitOptions = nodeInit.mock.calls[0]?.[0] as ModifiedInitOptions;
const consoleIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Console');
expect(consoleIntegration).toBeDefined();
});
});
it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
describe('OpenNext/Cloudflare runtime detection', () => {
const cloudflareContextSymbol = Symbol.for('__cloudflare-context__');
beforeEach(() => {
// Reset the global scope to allow re-initialization
SentryNode.getGlobalScope().clear();
SentryNode.getIsolationScope().clear();
SentryNode.getCurrentScope().clear();
SentryNode.getCurrentScope().setClient(undefined);
});
afterEach(() => {
// Clean up the cloudflare context
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete (GLOBAL_OBJ as unknown as Record<symbol, unknown>)[cloudflareContextSymbol];
});
it('sets cloudflare runtime when OpenNext context is available', () => {
// Mock the OpenNext Cloudflare context
(GLOBAL_OBJ as unknown as Record<symbol, unknown>)[cloudflareContextSymbol] = {
ctx: {
waitUntil: vi.fn(),
},
};
init({});
expect(nodeInit).toHaveBeenLastCalledWith(
expect.objectContaining({
runtime: { name: 'cloudflare' },
}),
);
});
it('sets cloudflare in SDK metadata when OpenNext context is available', () => {
// Mock the OpenNext Cloudflare context
(GLOBAL_OBJ as unknown as Record<symbol, unknown>)[cloudflareContextSymbol] = {
ctx: {
waitUntil: vi.fn(),
},
};
init({});
expect(nodeInit).toHaveBeenLastCalledWith(
expect.objectContaining({
_metadata: expect.objectContaining({
sdk: expect.objectContaining({
name: 'sentry.javascript.nextjs',
packages: expect.arrayContaining([
expect.objectContaining({
name: 'npm:@sentry/nextjs',
}),
expect.objectContaining({
name: 'npm:@sentry/cloudflare',
}),
]),
}),
}),
}),
);
});
it('does not set cloudflare runtime when OpenNext context is not available', () => {
init({});
expect(nodeInit).toHaveBeenLastCalledWith(
expect.not.objectContaining({
runtime: { name: 'cloudflare' },
}),
);
});
});
describe('environment option', () => {
const originalEnv = process.env.SENTRY_ENVIRONMENT;
const originalNodeEnv = process.env.NODE_ENV;
beforeEach(() => {
// Reset the global scope to allow re-initialization
SentryNode.getGlobalScope().clear();
SentryNode.getIsolationScope().clear();
SentryNode.getCurrentScope().clear();
SentryNode.getCurrentScope().setClient(undefined);
});
afterEach(() => {
if (originalEnv !== undefined) {
process.env.SENTRY_ENVIRONMENT = originalEnv;
} else {
delete process.env.SENTRY_ENVIRONMENT;
}
if (originalNodeEnv !== undefined) {
process.env.NODE_ENV = originalNodeEnv;
} else {
delete process.env.NODE_ENV;
}
});
it('uses environment from options when provided', () => {
delete process.env.SENTRY_ENVIRONMENT;
process.env.NODE_ENV = 'development';
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'custom-env',
});
expect(nodeInit).toHaveBeenCalledTimes(1);
const callArgs = nodeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('custom-env');
});
it('uses SENTRY_ENVIRONMENT env var when options.environment is not provided', () => {
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';
process.env.NODE_ENV = 'development';
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});
expect(nodeInit).toHaveBeenCalledTimes(1);
const callArgs = nodeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('env-from-variable');
});
it('uses NODE_ENV as fallback when neither options.environment nor SENTRY_ENVIRONMENT is provided', () => {
delete process.env.SENTRY_ENVIRONMENT;
process.env.NODE_ENV = 'production';
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});
expect(nodeInit).toHaveBeenCalledTimes(1);
const callArgs = nodeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('production');
});
it('prioritizes options.environment over SENTRY_ENVIRONMENT env var', () => {
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';
process.env.NODE_ENV = 'development';
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'options-env',
});
expect(nodeInit).toHaveBeenCalledTimes(1);
const callArgs = nodeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('options-env');
});
it('prioritizes SENTRY_ENVIRONMENT over NODE_ENV', () => {
process.env.SENTRY_ENVIRONMENT = 'sentry-env';
process.env.NODE_ENV = 'development';
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});
expect(nodeInit).toHaveBeenCalledTimes(1);
const callArgs = nodeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('sentry-env');
});
});
});