Skip to content

Commit c068724

Browse files
committed
handle deprecations in tests
1 parent 2f5c924 commit c068724

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

packages/core/src/tracing/trace.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { Hub } from '../hub';
1818
import { getCurrentHub } from '../hub';
1919
import { handleCallbackErrors } from '../utils/handleCallbackErrors';
2020
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
21-
import { spanTimeInputToSeconds } from '../utils/spanUtils';
21+
import { spanTimeInputToSeconds, spanToJSON } from '../utils/spanUtils';
2222

2323
interface StartSpanOptions extends TransactionContext {
2424
/** A manually specified start time for the created `Span` object. */
@@ -196,8 +196,11 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span |
196196
() => callback(activeSpan),
197197
() => {
198198
// Only update the span status if it hasn't been changed yet
199-
if (activeSpan && (!activeSpan.status || activeSpan.status === 'ok')) {
200-
activeSpan.setStatus('internal_error');
199+
if (activeSpan) {
200+
const { status } = spanToJSON(activeSpan);
201+
if (!status || status === 'ok') {
202+
activeSpan.setStatus('internal_error');
203+
}
201204
}
202205
},
203206
() => activeSpan && activeSpan.end(),
@@ -245,8 +248,11 @@ export function startSpanManual<T>(
245248
() => callback(activeSpan, finishAndSetSpan),
246249
() => {
247250
// Only update the span status if it hasn't been changed yet, and the span is not yet finished
248-
if (activeSpan && activeSpan.isRecording() && (!activeSpan.status || activeSpan.status === 'ok')) {
249-
activeSpan.setStatus('internal_error');
251+
if (activeSpan && activeSpan.isRecording()) {
252+
const { status } = spanToJSON(activeSpan);
253+
if (!status || status === 'ok') {
254+
activeSpan.setStatus('internal_error');
255+
}
250256
}
251257
},
252258
);

packages/core/test/lib/tracing/errors.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BrowserClient } from '@sentry/browser';
2-
import { Hub, addTracingExtensions, makeMain, startInactiveSpan, startSpan } from '@sentry/core';
2+
import { Hub, addTracingExtensions, makeMain, spanToJSON, startInactiveSpan, startSpan } from '@sentry/core';
33
import type { HandlerDataError, HandlerDataUnhandledRejection } from '@sentry/types';
44

55
import { getDefaultBrowserClientOptions } from '../../../../tracing/test/testutils';
@@ -51,13 +51,20 @@ describe('registerErrorHandlers()', () => {
5151
registerErrorInstrumentation();
5252

5353
const transaction = startInactiveSpan({ name: 'test' })!;
54+
// eslint-disable-next-line deprecation/deprecation
5455
expect(transaction.status).toBe(undefined);
56+
expect(spanToJSON(transaction).status).toBe(undefined);
5557

5658
mockErrorCallback({} as HandlerDataError);
59+
// eslint-disable-next-line deprecation/deprecation
5760
expect(transaction.status).toBe(undefined);
61+
expect(spanToJSON(transaction).status).toBe(undefined);
5862

5963
mockUnhandledRejectionCallback({});
64+
// eslint-disable-next-line deprecation/deprecation
6065
expect(transaction.status).toBe(undefined);
66+
expect(spanToJSON(transaction).status).toBe(undefined);
67+
6168
transaction.end();
6269
});
6370

@@ -66,7 +73,9 @@ describe('registerErrorHandlers()', () => {
6673

6774
startSpan({ name: 'test' }, span => {
6875
mockErrorCallback({} as HandlerDataError);
69-
expect(span?.status).toBe('internal_error');
76+
// eslint-disable-next-line deprecation/deprecation
77+
expect(span!.status).toBe('internal_error');
78+
expect(spanToJSON(span!).status).toBe(undefined);
7079
});
7180
});
7281

@@ -75,7 +84,9 @@ describe('registerErrorHandlers()', () => {
7584

7685
startSpan({ name: 'test' }, span => {
7786
mockUnhandledRejectionCallback({});
78-
expect(span?.status).toBe('internal_error');
87+
// eslint-disable-next-line deprecation/deprecation
88+
expect(span!.status).toBe('internal_error');
89+
expect(spanToJSON(span!).status).toBe(undefined);
7990
});
8091
});
8192
});

packages/opentelemetry-node/test/spanprocessor.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,15 @@ describe('SentrySpanProcessor', () => {
344344
const transaction = getSpanForOtelSpan(otelSpan) as Transaction;
345345

346346
// status is only set after end
347+
// eslint-disable-next-line deprecation/deprecation
347348
expect(transaction?.status).toBe(undefined);
349+
expect(spanToJSON(transaction!).status).toBe(undefined);
348350

349351
otelSpan.end();
350352

353+
// eslint-disable-next-line deprecation/deprecation
351354
expect(transaction?.status).toBe('ok');
355+
expect(spanToJSON(transaction!).status).toBe('ok');
352356
});
353357

354358
it('sets status for span', async () => {
@@ -358,11 +362,15 @@ describe('SentrySpanProcessor', () => {
358362
tracer.startActiveSpan('SELECT * FROM users;', child => {
359363
const sentrySpan = getSpanForOtelSpan(child);
360364

365+
// eslint-disable-next-line deprecation/deprecation
361366
expect(sentrySpan?.status).toBe(undefined);
367+
expect(spanToJSON(sentrySpan!).status).toBe(undefined);
362368

363369
child.end();
364370

371+
// eslint-disable-next-line deprecation/deprecation
365372
expect(sentrySpan?.status).toBe('ok');
373+
expect(spanToJSON(sentrySpan!).status).toBe('ok');
366374

367375
parentOtelSpan.end();
368376
});
@@ -444,7 +452,9 @@ describe('SentrySpanProcessor', () => {
444452
}
445453

446454
otelSpan.end();
455+
// eslint-disable-next-line deprecation/deprecation
447456
expect(transaction?.status).toBe(expected);
457+
expect(spanToJSON(transaction!).status).toBe(expected);
448458
},
449459
);
450460
});

packages/tracing-internal/src/browser/backgroundtab.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IdleTransaction, SpanStatusType } from '@sentry/core';
2-
import { getActiveTransaction } from '@sentry/core';
2+
import { getActiveTransaction, spanToJSON } from '@sentry/core';
33
import { logger } from '@sentry/utils';
44

55
import { DEBUG_BUILD } from '../common/debug-build';
@@ -23,7 +23,7 @@ export function registerBackgroundTabDetection(): void {
2323
);
2424
// We should not set status if it is already set, this prevent important statuses like
2525
// error or data loss from being overwritten on transaction.
26-
if (!activeTransaction.status) {
26+
if (!spanToJSON(activeTransaction).status) {
2727
activeTransaction.setStatus(statusType);
2828
}
2929
// TODO: Can we rewrite this to an attribute?

packages/tracing-internal/test/browser/backgroundtab.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ conditionalTest({ min: 10 })('registerBackgroundTabDetection', () => {
5555
global.document.hidden = true;
5656
events.visibilitychange();
5757

58+
const { status, timestamp } = spanToJSON(span!);
59+
60+
// eslint-disable-next-line deprecation/deprecation
5861
expect(span?.status).toBe('cancelled');
62+
expect(status).toBeDefined();
5963
// eslint-disable-next-line deprecation/deprecation
6064
expect(span?.tags.visibilitychange).toBe('document.hidden');
61-
expect(spanToJSON(span!).timestamp).toBeDefined();
65+
expect(timestamp).toBeDefined();
6266
});
6367
});
6468
});

0 commit comments

Comments
 (0)