Skip to content

Commit 70d642b

Browse files
committed
Reinstate scope tests
1 parent 346896f commit 70d642b

File tree

3 files changed

+121
-23
lines changed

3 files changed

+121
-23
lines changed

packages/node-core/rollup.anr-worker.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function createWorkerCodeBuilder(entry, outDir) {
77
makeBaseBundleConfig({
88
bundleType: 'node-worker',
99
entrypoints: [entry],
10-
licenseTitle: '@sentry/node',
10+
licenseTitle: '@sentry/node-core',
1111
outputFileBase: () => 'worker-script.js',
1212
packageSpecificConfig: {
1313
output: {

packages/node-core/src/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { init } from './sdk';
22

33
/**
4-
* The @sentry/node/init export can be used with the node --import and --require args to initialize the SDK entirely via
4+
* The @sentry/node-core/init export can be used with the node --import and --require args to initialize the SDK entirely via
55
* environment variables.
66
*
77
* > SENTRY_DSN=https://examplePublicKey@o0.ingest.sentry.io/0 SENTRY_TRACES_SAMPLE_RATE=1.0 node --import=@sentry/node/init app.mjs

packages/node-core/test/integration/scope.test.ts

Lines changed: 119 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ describe('Integration | Scope', () => {
1616
const beforeSend = vi.fn(() => null);
1717
const beforeSendTransaction = vi.fn(() => null);
1818

19-
const client = tracingEnabled
20-
? mockSdkInit({ tracesSampleRate: 1, beforeSend, beforeSendTransaction })
21-
: mockSdkInit({ beforeSend, beforeSendTransaction });
19+
const client = mockSdkInit({ tracesSampleRate: tracingEnabled ? 1 : 0, beforeSend, beforeSendTransaction });
2220

2321
const rootScope = getCurrentScope();
2422

@@ -38,29 +36,24 @@ describe('Integration | Scope', () => {
3836
Sentry.withScope(scope2 => {
3937
scope2.setTag('tag3', 'val3');
4038

41-
if (tracingEnabled) {
42-
Sentry.startSpan({ name: 'outer' }, span => {
43-
expect(getCapturedScopesOnSpan(span).scope).toBe(scope2);
44-
45-
spanId = span.spanContext().spanId;
46-
traceId = span.spanContext().traceId;
39+
Sentry.startSpan({ name: 'outer' }, span => {
40+
expect(getCapturedScopesOnSpan(span).scope).toBe(tracingEnabled ? scope2 : undefined);
4741

48-
Sentry.setTag('tag4', 'val4');
42+
spanId = span.spanContext().spanId;
43+
traceId = span.spanContext().traceId;
4944

50-
Sentry.captureException(error);
51-
});
52-
} else {
5345
Sentry.setTag('tag4', 'val4');
46+
5447
Sentry.captureException(error);
55-
}
48+
});
5649
});
5750
});
5851

5952
await client?.flush();
6053

6154
expect(beforeSend).toHaveBeenCalledTimes(1);
6255

63-
if (spanId && tracingEnabled) {
56+
if (spanId) {
6457
expect(beforeSend).toHaveBeenCalledWith(
6558
expect.objectContaining({
6659
contexts: expect.objectContaining({
@@ -135,9 +128,7 @@ describe('Integration | Scope', () => {
135128
const beforeSend = vi.fn(() => null);
136129
const beforeSendTransaction = vi.fn(() => null);
137130

138-
const client = tracingEnabled
139-
? mockSdkInit({ tracesSampleRate: 1, beforeSend, beforeSendTransaction })
140-
: mockSdkInit({ beforeSend, beforeSendTransaction });
131+
const client = mockSdkInit({ tracesSampleRate: tracingEnabled ? 1 : 0, beforeSend, beforeSendTransaction });
141132

142133
const rootScope = getCurrentScope();
143134

@@ -235,17 +226,124 @@ describe('Integration | Scope', () => {
235226
);
236227

237228
if (tracingEnabled) {
238-
expect(traceId1).not.toEqual(traceId2);
239229
expect(beforeSendTransaction).toHaveBeenCalledTimes(2);
240230
}
241231
});
242232
});
243233

234+
describe('global scope', () => {
235+
beforeEach(() => {
236+
resetGlobals();
237+
});
238+
239+
it('works before calling init', () => {
240+
const globalScope = Sentry.getGlobalScope();
241+
expect(globalScope).toBeDefined();
242+
// No client attached
243+
expect(globalScope.getClient()).toBeUndefined();
244+
// Repeatedly returns the same instance
245+
expect(Sentry.getGlobalScope()).toBe(globalScope);
246+
247+
globalScope.setTag('tag1', 'val1');
248+
globalScope.setTag('tag2', 'val2');
249+
250+
expect(globalScope.getScopeData().tags).toEqual({ tag1: 'val1', tag2: 'val2' });
251+
252+
// Now when we call init, the global scope remains intact
253+
Sentry.init({ dsn: 'https://username@domain/123', defaultIntegrations: false });
254+
255+
expect(globalScope.getClient()).toBeUndefined();
256+
expect(Sentry.getGlobalScope()).toBe(globalScope);
257+
expect(globalScope.getScopeData().tags).toEqual({ tag1: 'val1', tag2: 'val2' });
258+
});
259+
260+
it('is applied to events', async () => {
261+
const beforeSend = vi.fn();
262+
const client = mockSdkInit({ beforeSend });
263+
264+
const globalScope = Sentry.getGlobalScope();
265+
globalScope.setTag('tag1', 'val1');
266+
globalScope.setTag('tag2', 'val2');
267+
268+
const error = new Error('test error');
269+
Sentry.captureException(error);
270+
271+
await client?.flush();
272+
273+
expect(beforeSend).toHaveBeenCalledTimes(1);
274+
expect(beforeSend).toHaveBeenCalledWith(
275+
expect.objectContaining({
276+
tags: {
277+
tag1: 'val1',
278+
tag2: 'val2',
279+
},
280+
}),
281+
{
282+
event_id: expect.any(String),
283+
originalException: error,
284+
syntheticException: expect.any(Error),
285+
},
286+
);
287+
});
288+
});
289+
244290
describe('isolation scope', () => {
245291
beforeEach(() => {
246292
resetGlobals();
247293
});
248294

295+
it('works before calling init', () => {
296+
const isolationScope = Sentry.getIsolationScope();
297+
expect(isolationScope).toBeDefined();
298+
// No client attached
299+
expect(isolationScope.getClient()).toBeUndefined();
300+
// Repeatedly returns the same instance
301+
expect(Sentry.getIsolationScope()).toBe(isolationScope);
302+
303+
isolationScope.setTag('tag1', 'val1');
304+
isolationScope.setTag('tag2', 'val2');
305+
306+
expect(isolationScope.getScopeData().tags).toEqual({ tag1: 'val1', tag2: 'val2' });
307+
308+
// Now when we call init, the isolation scope remains intact
309+
Sentry.init({ dsn: 'https://username@domain/123', defaultIntegrations: false });
310+
311+
// client is only attached to global scope by default
312+
expect(isolationScope.getClient()).toBeUndefined();
313+
expect(Sentry.getIsolationScope()).toBe(isolationScope);
314+
expect(isolationScope.getScopeData().tags).toEqual({ tag1: 'val1', tag2: 'val2' });
315+
});
316+
317+
it('is applied to events', async () => {
318+
const beforeSend = vi.fn();
319+
mockSdkInit({ beforeSend });
320+
const client = Sentry.getClient();
321+
322+
const isolationScope = Sentry.getIsolationScope();
323+
isolationScope.setTag('tag1', 'val1');
324+
isolationScope.setTag('tag2', 'val2');
325+
326+
const error = new Error('test error');
327+
Sentry.captureException(error);
328+
329+
await client?.flush();
330+
331+
expect(beforeSend).toHaveBeenCalledTimes(1);
332+
expect(beforeSend).toHaveBeenCalledWith(
333+
expect.objectContaining({
334+
tags: {
335+
tag1: 'val1',
336+
tag2: 'val2',
337+
},
338+
}),
339+
{
340+
event_id: expect.any(String),
341+
originalException: error,
342+
syntheticException: expect.any(Error),
343+
},
344+
);
345+
});
346+
249347
it('withIsolationScope works', async () => {
250348
const beforeSend = vi.fn();
251349
mockSdkInit({ beforeSend });
@@ -494,8 +592,8 @@ describe('Integration | Scope', () => {
494592
});
495593

496594
it('automatically forks with OTEL context', async () => {
497-
const beforeSend = vi.fn(() => null);
498-
mockSdkInit({ tracesSampleRate: 1, beforeSend });
595+
const beforeSend = vi.fn();
596+
mockSdkInit({ beforeSend });
499597
const client = Sentry.getClient();
500598

501599
const initialCurrentScope = Sentry.getCurrentScope();

0 commit comments

Comments
 (0)