Skip to content

Commit 2fba037

Browse files
committed
add another test for scope attribute precedence over contexts
1 parent 282eed0 commit 2fba037

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

packages/core/test/lib/spans/captureSpan.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { beforeEach, describe, expect, it, test, vi } from 'vitest';
22
import type { Client } from '../../../src';
33
import {
44
getCurrentScope,
@@ -322,4 +322,51 @@ describe('captureSpan', () => {
322322
}),
323323
);
324324
});
325+
326+
test('scope attributes have precedence over attributes derived from contexts', () => {
327+
currentScope.setUser({ id: '123', email: 'user@example.com', username: 'testuser' });
328+
329+
const span = new SentrySpan({ name: 'spanName' });
330+
setCapturedScopesOnSpan(span, currentScope, isolationScope);
331+
332+
// Aalthough the current scope has precedence over the global scope,
333+
// scope attributes have precedence over context attributes
334+
getGlobalScope().setAttribute('app.name', 'myApp-scope-attribute');
335+
currentScope.setContext('app', { name: 'myApp-current-scope-context' });
336+
337+
captureSpan(span, client);
338+
339+
expect(enqueueSpanCallback).toHaveBeenCalledOnce();
340+
expect(enqueueSpanCallback).toHaveBeenCalledWith(
341+
expect.objectContaining({
342+
attributes: {
343+
'sentry.environment': {
344+
type: 'string',
345+
value: 'staging',
346+
},
347+
'sentry.origin': {
348+
type: 'string',
349+
value: 'manual',
350+
},
351+
'sentry.release': {
352+
type: 'string',
353+
value: '1.1.1',
354+
},
355+
'sentry.segment.id': {
356+
type: 'string',
357+
value: span.spanContext().spanId,
358+
},
359+
'sentry.segment.name': {
360+
type: 'string',
361+
value: 'spanName',
362+
},
363+
// Therefore, we expect the attribute to be taken from the global scope's attributes
364+
'app.name': {
365+
type: 'string',
366+
value: 'myApp-scope-attribute',
367+
},
368+
},
369+
}),
370+
);
371+
});
325372
});

0 commit comments

Comments
 (0)