|
1 | | -import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 1 | +import { beforeEach, describe, expect, it, test, vi } from 'vitest'; |
2 | 2 | import type { Client } from '../../../src'; |
3 | 3 | import { |
4 | 4 | getCurrentScope, |
@@ -322,4 +322,51 @@ describe('captureSpan', () => { |
322 | 322 | }), |
323 | 323 | ); |
324 | 324 | }); |
| 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 | + }); |
325 | 372 | }); |
0 commit comments