-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathclientSdk.test.ts
More file actions
276 lines (225 loc) · 9.53 KB
/
clientSdk.test.ts
File metadata and controls
276 lines (225 loc) · 9.53 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
import type { Integration } from '@sentry/core';
import { debug, getGlobalScope, getIsolationScope } from '@sentry/core';
import * as SentryReact from '@sentry/react';
import { getClient, getCurrentScope, WINDOW } from '@sentry/react';
import { JSDOM } from 'jsdom';
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest';
import { breadcrumbsIntegration, browserTracingIntegration, init } from '../src/client';
const reactInit = vi.spyOn(SentryReact, 'init');
const debugLogSpy = vi.spyOn(debug, 'log');
// We're setting up JSDom here because the Next.js routing instrumentations requires a few things to be present on pageload:
// 1. Access to window.document API for `window.document.getElementById`
// 2. Access to window.location API for `window.location.pathname`
const dom = new JSDOM(undefined, { url: 'https://example.com/' });
Object.defineProperty(global, 'document', { value: dom.window.document, writable: true });
Object.defineProperty(global, 'location', { value: dom.window.document.location, writable: true });
Object.defineProperty(global, 'addEventListener', { value: () => undefined, writable: true });
const originalGlobalDocument = WINDOW.document;
const originalGlobalLocation = WINDOW.location;
const originalNavigator = WINDOW.navigator;
// eslint-disable-next-line @typescript-eslint/unbound-method
const originalGlobalAddEventListener = WINDOW.addEventListener;
afterAll(() => {
// Clean up JSDom
Object.defineProperty(WINDOW, 'document', { value: originalGlobalDocument });
Object.defineProperty(WINDOW, 'location', { value: originalGlobalLocation });
Object.defineProperty(WINDOW, 'navigator', { value: originalNavigator, writable: true, configurable: true });
Object.defineProperty(WINDOW, 'addEventListener', { value: originalGlobalAddEventListener });
});
function findIntegrationByName(integrations: Integration[] = [], name: string): Integration | undefined {
return integrations.find(integration => integration.name === name);
}
const TEST_DSN = 'https://public@dsn.ingest.sentry.io/1337';
describe('Client init()', () => {
afterEach(() => {
vi.clearAllMocks();
getGlobalScope().clear();
getIsolationScope().clear();
getCurrentScope().clear();
getCurrentScope().setClient(undefined);
Object.defineProperty(WINDOW, 'navigator', { value: originalNavigator, writable: true, configurable: true });
});
it('inits the React SDK', () => {
expect(reactInit).toHaveBeenCalledTimes(0);
init({});
expect(reactInit).toHaveBeenCalledTimes(1);
expect(reactInit).toHaveBeenCalledWith(
expect.objectContaining({
_metadata: {
sdk: {
name: 'sentry.javascript.nextjs',
version: expect.any(String),
packages: [
{
name: 'npm:@sentry/nextjs',
version: expect.any(String),
},
{
name: 'npm:@sentry/react',
version: expect.any(String),
},
],
settings: {
infer_ip: 'never',
},
},
},
environment: 'test',
defaultIntegrations: expect.arrayContaining([
expect.objectContaining({
name: 'NextjsClientStackFrameNormalization',
}),
]),
}),
);
});
it('adds 404 transaction filter', () => {
init({
dsn: 'https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012',
tracesSampleRate: 1.0,
});
const transportSend = vi.spyOn(getClient()!.getTransport()!, 'send');
// Ensure we have no current span, so our next span is a transaction
SentryReact.withActiveSpan(null, () => {
SentryReact.startInactiveSpan({ name: '/404' })?.end();
});
expect(transportSend).not.toHaveBeenCalled();
expect(debugLogSpy).toHaveBeenCalledWith('An event processor returned `null`, will not send event.');
});
describe('integrations', () => {
// Options passed by `@sentry/nextjs`'s `init` to `@sentry/react`'s `init` after modifying them
type ModifiedInitOptionsIntegrationArray = { defaultIntegrations: Integration[]; integrations: Integration[] };
it('supports passing unrelated integrations through options', () => {
init({ integrations: [breadcrumbsIntegration({ console: false })] });
const reactInitOptions = reactInit.mock.calls[0]![0] as ModifiedInitOptionsIntegrationArray;
const installedBreadcrumbsIntegration = findIntegrationByName(reactInitOptions.integrations, 'Breadcrumbs');
expect(installedBreadcrumbsIntegration).toBeDefined();
});
it('forces correct router instrumentation if user provides `browserTracingIntegration` in an array', () => {
const providedBrowserTracingInstance = browserTracingIntegration();
const client = init({
dsn: TEST_DSN,
tracesSampleRate: 1.0,
integrations: [providedBrowserTracingInstance],
});
const integration = client?.getIntegrationByName('BrowserTracing');
expect(integration).toBe(providedBrowserTracingInstance);
});
it('forces correct router instrumentation if user provides `BrowserTracing` in a function', () => {
const providedBrowserTracingInstance = browserTracingIntegration();
const client = init({
dsn: TEST_DSN,
tracesSampleRate: 1.0,
integrations: defaults => [...defaults, providedBrowserTracingInstance],
});
const integration = client?.getIntegrationByName('BrowserTracing');
expect(integration).toBe(providedBrowserTracingInstance);
});
describe('browserTracingIntegration()', () => {
it('adds the browserTracingIntegration when `__SENTRY_TRACING__` is not set', () => {
const client = init({
dsn: TEST_DSN,
});
const browserTracingIntegration = client?.getIntegrationByName('BrowserTracing');
expect(browserTracingIntegration).toBeDefined();
});
it("doesn't add a browserTracingIntegration if `__SENTRY_TRACING__` is set to false", () => {
// @ts-expect-error Test setup for build-time flag
globalThis.__SENTRY_TRACING__ = false;
const client = init({
dsn: TEST_DSN,
});
const browserTracingIntegration = client?.getIntegrationByName('BrowserTracing');
expect(browserTracingIntegration).toBeUndefined();
// @ts-expect-error Test setup for build-time flag
delete globalThis.__SENTRY_TRACING__;
});
it("doesn't run Next.js router instrumentation for bot user agents", () => {
Object.defineProperty(WINDOW, 'navigator', {
value: {
userAgent: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
},
writable: true,
configurable: true,
});
const setIntervalSpy = vi.spyOn(globalThis, 'setInterval');
init({
dsn: TEST_DSN,
tracesSampleRate: 1.0,
});
expect(setIntervalSpy).not.toHaveBeenCalled();
});
});
});
it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
describe('environment option', () => {
const originalEnv = process.env.SENTRY_ENVIRONMENT;
const originalNodeEnv = process.env.NODE_ENV;
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: TEST_DSN,
environment: 'custom-env',
});
expect(reactInit).toHaveBeenCalledTimes(1);
const callArgs = reactInit.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: TEST_DSN,
});
expect(reactInit).toHaveBeenCalledTimes(1);
const callArgs = reactInit.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: TEST_DSN,
});
expect(reactInit).toHaveBeenCalledTimes(1);
const callArgs = reactInit.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: TEST_DSN,
environment: 'options-env',
});
expect(reactInit).toHaveBeenCalledTimes(1);
const callArgs = reactInit.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: TEST_DSN,
});
expect(reactInit).toHaveBeenCalledTimes(1);
const callArgs = reactInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('sentry-env');
});
});
});