forked from getsentry/sentry-javascript
/
index.client.test.ts
197 lines (163 loc) · 7.64 KB
/
index.client.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
192
193
194
195
196
197
import { BaseClient, getCurrentHub } from '@sentry/core';
import * as SentryReact from '@sentry/react';
import { WINDOW } from '@sentry/react';
import { Integrations as TracingIntegrations } from '@sentry/tracing';
import { Integration } from '@sentry/types';
import { logger } from '@sentry/utils';
import { JSDOM } from 'jsdom';
import { init, Integrations, nextRouterInstrumentation } from '../src/index.client';
import { UserIntegrationsFunction } from '../src/utils/userIntegrations';
const { BrowserTracing } = TracingIntegrations;
const reactInit = jest.spyOn(SentryReact, 'init');
const captureEvent = jest.spyOn(BaseClient.prototype, 'captureEvent');
const loggerLogSpy = jest.spyOn(logger, '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 });
const originalGlobalDocument = WINDOW.document;
const originalGlobalLocation = WINDOW.location;
afterAll(() => {
// Clean up JSDom
Object.defineProperty(WINDOW, 'document', { value: originalGlobalDocument });
Object.defineProperty(WINDOW, 'location', { value: originalGlobalLocation });
});
function findIntegrationByName(integrations: Integration[] = [], name: string): Integration | undefined {
return integrations.find(integration => integration.name === name);
}
describe('Client init()', () => {
afterEach(() => {
jest.clearAllMocks();
WINDOW.__SENTRY__.hub = undefined;
});
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),
},
],
},
},
environment: 'test',
integrations: undefined,
}),
);
});
it('sets runtime on scope', () => {
const currentScope = getCurrentHub().getScope();
// @ts-ignore need access to protected _tags attribute
expect(currentScope._tags).toEqual({});
init({});
// @ts-ignore need access to protected _tags attribute
expect(currentScope._tags).toEqual({ runtime: 'browser' });
});
it('adds 404 transaction filter', () => {
init({
dsn: 'https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012',
tracesSampleRate: 1.0,
});
const hub = getCurrentHub();
const transportSend = jest.spyOn(hub.getClient()!.getTransport()!, 'send');
const transaction = hub.startTransaction({ name: '/404' });
transaction.finish();
expect(transportSend).not.toHaveBeenCalled();
expect(captureEvent.mock.results[0].value).toBeUndefined();
expect(loggerLogSpy).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 = { integrations: Integration[] };
type ModifiedInitOptionsIntegrationFunction = { integrations: UserIntegrationsFunction };
it('supports passing unrelated integrations through options', () => {
init({ integrations: [new Integrations.Breadcrumbs({ console: false })] });
const reactInitOptions = reactInit.mock.calls[0][0] as ModifiedInitOptionsIntegrationArray;
const breadcrumbsIntegration = findIntegrationByName(reactInitOptions.integrations, 'Breadcrumbs');
expect(breadcrumbsIntegration).toBeDefined();
});
describe('`BrowserTracing` integration', () => {
it('adds `BrowserTracing` integration if `tracesSampleRate` is set', () => {
init({ tracesSampleRate: 1.0 });
const reactInitOptions = reactInit.mock.calls[0][0] as ModifiedInitOptionsIntegrationArray;
const browserTracingIntegration = findIntegrationByName(reactInitOptions.integrations, 'BrowserTracing');
expect(browserTracingIntegration).toBeDefined();
expect(browserTracingIntegration).toEqual(
expect.objectContaining({
options: expect.objectContaining({
routingInstrumentation: nextRouterInstrumentation,
}),
}),
);
});
it('adds `BrowserTracing` integration if `tracesSampler` is set', () => {
init({ tracesSampler: () => true });
const reactInitOptions = reactInit.mock.calls[0][0] as ModifiedInitOptionsIntegrationArray;
const browserTracingIntegration = findIntegrationByName(reactInitOptions.integrations, 'BrowserTracing');
expect(browserTracingIntegration).toBeDefined();
expect(browserTracingIntegration).toEqual(
expect.objectContaining({
options: expect.objectContaining({
routingInstrumentation: nextRouterInstrumentation,
}),
}),
);
});
it('does not add `BrowserTracing` integration if tracing not enabled in SDK', () => {
init({});
const reactInitOptions = reactInit.mock.calls[0][0] as ModifiedInitOptionsIntegrationArray;
const browserTracingIntegration = findIntegrationByName(reactInitOptions.integrations, 'BrowserTracing');
expect(browserTracingIntegration).toBeUndefined();
});
it('forces correct router instrumentation if user provides `BrowserTracing` in an array', () => {
init({
tracesSampleRate: 1.0,
integrations: [new BrowserTracing({ startTransactionOnLocationChange: false })],
});
const reactInitOptions = reactInit.mock.calls[0][0] as ModifiedInitOptionsIntegrationArray;
const browserTracingIntegration = findIntegrationByName(reactInitOptions.integrations, 'BrowserTracing');
expect(browserTracingIntegration).toEqual(
expect.objectContaining({
options: expect.objectContaining({
routingInstrumentation: nextRouterInstrumentation,
// This proves it's still the user's copy
startTransactionOnLocationChange: false,
}),
}),
);
});
it('forces correct router instrumentation if user provides `BrowserTracing` in a function', () => {
init({
tracesSampleRate: 1.0,
integrations: defaults => [...defaults, new BrowserTracing({ startTransactionOnLocationChange: false })],
});
const reactInitOptions = reactInit.mock.calls[0][0] as ModifiedInitOptionsIntegrationFunction;
const materializedIntegrations = reactInitOptions.integrations(SentryReact.defaultIntegrations);
const browserTracingIntegration = findIntegrationByName(materializedIntegrations, 'BrowserTracing');
expect(browserTracingIntegration).toEqual(
expect.objectContaining({
options: expect.objectContaining({
routingInstrumentation: nextRouterInstrumentation,
// This proves it's still the user's copy
startTransactionOnLocationChange: false,
}),
}),
);
});
});
});
});