/
index.server.test.ts
233 lines (181 loc) · 8.47 KB
/
index.server.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
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
import * as SentryNode from '@sentry/node';
import { getCurrentHub, NodeClient } from '@sentry/node';
import { Integration } from '@sentry/types';
import { GLOBAL_OBJ, logger } from '@sentry/utils';
import * as domain from 'domain';
import { init } from '../src/index.server';
const { Integrations } = SentryNode;
// normally this is set as part of the build process, so mock it here
(GLOBAL_OBJ as typeof GLOBAL_OBJ & { __rewriteFramesDistDir__: string }).__rewriteFramesDistDir__ = '.next';
const nodeInit = jest.spyOn(SentryNode, 'init');
const loggerLogSpy = jest.spyOn(logger, 'log');
function findIntegrationByName(integrations: Integration[] = [], name: string): Integration | undefined {
return integrations.find(integration => integration.name === name);
}
describe('Server init()', () => {
afterEach(() => {
jest.clearAllMocks();
GLOBAL_OBJ.__SENTRY__.hub = 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),
},
],
},
},
autoSessionTracking: false,
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 upgrde 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.
integrations: 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);
});
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: 'node' });
});
// 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 = getCurrentHub().getScope();
expect(process.env.VERCEL).toBeUndefined();
init({});
// @ts-ignore need access to protected _tags attribute
expect(currentScope._tags.vercel).toBeUndefined();
});
it('adds 404 transaction filter', async () => {
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();
// We need to flush because the event processor pipeline is async whereas transaction.finish() is sync.
await SentryNode.flush();
expect(transportSend).not.toHaveBeenCalled();
expect(loggerLogSpy).toHaveBeenCalledWith('An event processor returned `null`, will not send event.');
});
it("initializes both global hub and domain hub when there's an active domain", () => {
const globalHub = getCurrentHub();
const local = domain.create();
local.run(() => {
const domainHub = getCurrentHub();
// they are in fact two different hubs, and neither one yet has a client
expect(domainHub).not.toBe(globalHub);
expect(globalHub.getClient()).toBeUndefined();
expect(domainHub.getClient()).toBeUndefined();
// this tag should end up only in the domain hub
domainHub.setTag('dogs', 'areGreat');
init({});
expect(globalHub.getClient()).toEqual(expect.any(NodeClient));
expect(domainHub.getClient()).toBe(globalHub.getClient());
// @ts-ignore need access to protected _tags attribute
expect(globalHub.getScope()._tags).toEqual({ runtime: 'node' });
// @ts-ignore need access to protected _tags attribute
expect(domainHub.getScope()._tags).toEqual({ runtime: 'node', dogs: 'areGreat' });
});
});
describe('integrations', () => {
// Options passed by `@sentry/nextjs`'s `init` to `@sentry/node`'s `init` after modifying them
type ModifiedInitOptions = { integrations: Integration[] };
it('adds default integrations', () => {
init({});
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
const rewriteFramesIntegration = findIntegrationByName(nodeInitOptions.integrations, 'RewriteFrames');
expect(rewriteFramesIntegration).toBeDefined();
});
it('supports passing unrelated integrations through options', () => {
init({ integrations: [new Integrations.Console()] });
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
const consoleIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Console');
expect(consoleIntegration).toBeDefined();
});
describe('`Http` integration', () => {
it('adds `Http` integration with tracing enabled if `tracesSampleRate` is set', () => {
init({ tracesSampleRate: 1.0 });
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
const httpIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Http');
expect(httpIntegration).toBeDefined();
expect(httpIntegration).toEqual(expect.objectContaining({ _tracing: {} }));
});
it('adds `Http` integration with tracing enabled if `tracesSampler` is set', () => {
init({ tracesSampler: () => true });
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
const httpIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Http');
expect(httpIntegration).toBeDefined();
expect(httpIntegration).toEqual(expect.objectContaining({ _tracing: {} }));
});
it('does not add `Http` integration if tracing not enabled in SDK', () => {
init({});
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
const httpIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Http');
expect(httpIntegration).toBeUndefined();
});
it('forces `_tracing = true` if `tracesSampleRate` is set', () => {
init({
tracesSampleRate: 1.0,
integrations: [new Integrations.Http({ tracing: false })],
});
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
const httpIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Http');
expect(httpIntegration).toBeDefined();
expect(httpIntegration).toEqual(expect.objectContaining({ _tracing: {} }));
});
it('forces `_tracing = true` if `tracesSampler` is set', () => {
init({
tracesSampler: () => true,
integrations: [new Integrations.Http({ tracing: false })],
});
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
const httpIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Http');
expect(httpIntegration).toBeDefined();
expect(httpIntegration).toEqual(expect.objectContaining({ _tracing: {} }));
});
it('does not force `_tracing = true` if tracing not enabled in SDK', () => {
init({
integrations: [new Integrations.Http({ tracing: false })],
});
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
const httpIntegration = findIntegrationByName(nodeInitOptions.integrations, 'Http');
expect(httpIntegration).toBeDefined();
expect(httpIntegration).toEqual(expect.objectContaining({ _tracing: false }));
});
});
});
});