-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathedgeSdk.test.ts
More file actions
140 lines (113 loc) · 4.8 KB
/
edgeSdk.test.ts
File metadata and controls
140 lines (113 loc) · 4.8 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
import type { Integration } from '@sentry/core';
import { GLOBAL_OBJ } from '@sentry/core';
import * as SentryVercelEdge from '@sentry/vercel-edge';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { init } from '../src/edge';
// 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 vercelEdgeInit = vi.spyOn(SentryVercelEdge, 'init');
function findIntegrationByName(integrations: Integration[] = [], name: string): Integration | undefined {
return integrations.find(integration => integration.name === name);
}
describe('Edge init()', () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.clearAllMocks();
});
it('inits the Vercel Edge SDK', () => {
expect(vercelEdgeInit).toHaveBeenCalledTimes(0);
init({});
expect(vercelEdgeInit).toHaveBeenCalledTimes(1);
expect(vercelEdgeInit).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/vercel-edge',
version: expect.any(String),
},
],
},
},
// 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.
defaultIntegrations: expect.any(Array),
}),
);
});
describe('integrations', () => {
// Options passed by `@sentry/nextjs`'s `init` to `@sentry/vercel-edge`'s `init` after modifying them
type ModifiedInitOptions = { integrations?: Integration[]; defaultIntegrations: Integration[] };
it('adds default integrations', () => {
init({});
const vercelEdgeInitOptions = vercelEdgeInit.mock.calls[0]?.[0] as ModifiedInitOptions;
const integrationNames = vercelEdgeInitOptions.defaultIntegrations.map(integration => integration.name);
expect(integrationNames).toContain('DistDirRewriteFrames');
});
it('supports passing unrelated integrations through options', () => {
init({ integrations: [SentryVercelEdge.dedupeIntegration()] });
const vercelEdgeInitOptions = vercelEdgeInit.mock.calls[0]?.[0] as ModifiedInitOptions;
const dedupeIntegration = findIntegrationByName(vercelEdgeInitOptions.integrations, 'Dedupe');
expect(dedupeIntegration).toBeDefined();
});
});
describe('environment option', () => {
const originalEnv = process.env.SENTRY_ENVIRONMENT;
afterEach(() => {
if (originalEnv !== undefined) {
process.env.SENTRY_ENVIRONMENT = originalEnv;
} else {
delete process.env.SENTRY_ENVIRONMENT;
}
});
it('uses environment from options when provided', () => {
delete process.env.SENTRY_ENVIRONMENT;
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'custom-env',
});
expect(vercelEdgeInit).toHaveBeenCalledTimes(1);
const callArgs = vercelEdgeInit.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';
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});
expect(vercelEdgeInit).toHaveBeenCalledTimes(1);
const callArgs = vercelEdgeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('env-from-variable');
});
it('uses fallback environment when neither options.environment nor SENTRY_ENVIRONMENT is provided', () => {
delete process.env.SENTRY_ENVIRONMENT;
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
});
expect(vercelEdgeInit).toHaveBeenCalledTimes(1);
const callArgs = vercelEdgeInit.mock.calls[0]?.[0];
// In test environment, it should be undefined since we only set it explicitly when SENTRY_ENVIRONMENT is present
// Note: The underlying vercel-edge SDK may set its own default
expect(callArgs?.environment).toBeDefined();
});
it('prioritizes options.environment over SENTRY_ENVIRONMENT env var', () => {
process.env.SENTRY_ENVIRONMENT = 'env-from-variable';
init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
environment: 'options-env',
});
expect(vercelEdgeInit).toHaveBeenCalledTimes(1);
const callArgs = vercelEdgeInit.mock.calls[0]?.[0];
expect(callArgs?.environment).toBe('options-env');
});
});
});