Skip to content

Commit 6aa5ccf

Browse files
committed
more test fixes
1 parent 45f83eb commit 6aa5ccf

File tree

1 file changed

+7
-184
lines changed

1 file changed

+7
-184
lines changed

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

Lines changed: 7 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { timestampInSeconds } from '@sentry/utils';
22
import { SentrySpan } from '../../../src';
3-
import { TRACE_FLAG_NONE, TRACE_FLAG_SAMPLED, spanToJSON } from '../../../src/utils/spanUtils';
3+
import { TRACE_FLAG_NONE, TRACE_FLAG_SAMPLED, spanToJSON, spanToTraceContext } from '../../../src/utils/spanUtils';
44

55
describe('span', () => {
66
describe('name', () => {
7-
/* eslint-disable deprecation/deprecation */
87
it('works with name', () => {
98
const span = new SentrySpan({ name: 'span name' });
109
expect(spanToJSON(span).description).toEqual('span name');
@@ -23,46 +22,15 @@ describe('span', () => {
2322
describe('new SentrySpan', () => {
2423
test('simple', () => {
2524
const span = new SentrySpan({ sampled: true });
25+
// eslint-disable-next-line deprecation/deprecation
2626
const span2 = span.startChild();
2727
expect((span2 as any).parentSpanId).toBe((span as any).spanId);
2828
expect((span2 as any).traceId).toBe((span as any).traceId);
2929
expect((span2 as any).sampled).toBe((span as any).sampled);
3030
});
31-
32-
test('sets instrumenter to `sentry` if not specified in constructor', () => {
33-
const span = new SentrySpan({});
34-
35-
expect(span.instrumenter).toBe('sentry');
36-
});
37-
38-
test('allows to set instrumenter in constructor', () => {
39-
const span = new SentrySpan({ instrumenter: 'otel' });
40-
41-
expect(span.instrumenter).toBe('otel');
42-
});
4331
});
4432

4533
describe('setters', () => {
46-
test('setTag', () => {
47-
const span = new SentrySpan({});
48-
expect(span.tags.foo).toBeUndefined();
49-
span.setTag('foo', 'bar');
50-
expect(span.tags.foo).toBe('bar');
51-
span.setTag('foo', 'baz');
52-
expect(span.tags.foo).toBe('baz');
53-
});
54-
55-
test('setData', () => {
56-
const span = new SentrySpan({});
57-
expect(span.data.foo).toBeUndefined();
58-
span.setData('foo', null);
59-
expect(span.data.foo).toBe(null);
60-
span.setData('foo', 2);
61-
expect(span.data.foo).toBe(2);
62-
span.setData('foo', true);
63-
expect(span.data.foo).toBe(true);
64-
});
65-
6634
test('setName', () => {
6735
const span = new SentrySpan({});
6836
expect(spanToJSON(span).description).toBeUndefined();
@@ -75,23 +43,14 @@ describe('span', () => {
7543
test('setStatus', () => {
7644
const span = new SentrySpan({});
7745
span.setStatus('permission_denied');
78-
expect((span.getTraceContext() as any).status).toBe('permission_denied');
79-
});
80-
81-
// TODO (v8): Remove
82-
test('setHttpStatus', () => {
83-
const span = new SentrySpan({});
84-
span.setHttpStatus(404);
85-
expect((span.getTraceContext() as any).status).toBe('not_found');
86-
expect(span.tags['http.status_code']).toBe('404');
87-
expect(span.data['http.response.status_code']).toBe(404);
46+
expect(spanToTraceContext(span).status).toBe('permission_denied');
8847
});
8948
});
9049

9150
describe('toJSON', () => {
9251
test('simple', () => {
93-
const span = JSON.parse(
94-
JSON.stringify(new SentrySpan({ traceId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', spanId: 'bbbbbbbbbbbbbbbb' })),
52+
const span = spanToJSON(
53+
new SentrySpan({ traceId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', spanId: 'bbbbbbbbbbbbbbbb' }),
9554
);
9655
expect(span).toHaveProperty('span_id', 'bbbbbbbbbbbbbbbb');
9756
expect(span).toHaveProperty('trace_id', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
@@ -100,7 +59,7 @@ describe('span', () => {
10059
test('with parent', () => {
10160
const spanA = new SentrySpan({ traceId: 'a', spanId: 'b' }) as any;
10261
const spanB = new SentrySpan({ traceId: 'c', spanId: 'd', sampled: false, parentSpanId: spanA.spanId });
103-
const serialized = JSON.parse(JSON.stringify(spanB));
62+
const serialized = spanToJSON(spanB);
10463
expect(serialized).toHaveProperty('parent_span_id', 'b');
10564
expect(serialized).toHaveProperty('span_id', 'd');
10665
expect(serialized).toHaveProperty('trace_id', 'c');
@@ -113,7 +72,7 @@ describe('span', () => {
11372
spanId: 'd',
11473
traceId: 'c',
11574
});
116-
const serialized = spanB.toJSON();
75+
const serialized = spanToJSON(spanB);
11776
expect(serialized).toStrictEqual({
11877
start_timestamp: expect.any(Number),
11978
parent_span_id: 'b',
@@ -161,142 +120,6 @@ describe('span', () => {
161120
});
162121
});
163122

164-
describe('getTraceContext', () => {
165-
test('should have status attribute undefined if no status tag is available', () => {
166-
const span = new SentrySpan({});
167-
const context = span.getTraceContext();
168-
expect((context as any).status).toBeUndefined();
169-
});
170-
171-
test('should have success status extracted from tags', () => {
172-
const span = new SentrySpan({});
173-
span.setStatus('ok');
174-
const context = span.getTraceContext();
175-
expect((context as any).status).toBe('ok');
176-
});
177-
178-
test('should have failure status extracted from tags', () => {
179-
const span = new SentrySpan({});
180-
span.setStatus('resource_exhausted');
181-
const context = span.getTraceContext();
182-
expect((context as any).status).toBe('resource_exhausted');
183-
});
184-
185-
test('should drop all `undefined` values', () => {
186-
const spanB = new SentrySpan({ spanId: 'd', traceId: 'c' });
187-
const context = spanB.getTraceContext();
188-
expect(context).toStrictEqual({
189-
span_id: 'd',
190-
trace_id: 'c',
191-
data: {
192-
'sentry.origin': 'manual',
193-
},
194-
origin: 'manual',
195-
});
196-
});
197-
});
198-
199-
describe('toContext and updateWithContext', () => {
200-
test('toContext should return correct context', () => {
201-
const originalContext = {
202-
traceId: 'a',
203-
spanId: 'b',
204-
sampled: false,
205-
description: 'test',
206-
op: 'op',
207-
};
208-
const span = new SentrySpan(originalContext);
209-
210-
const newContext = span.toContext();
211-
212-
expect(newContext).toStrictEqual({
213-
...originalContext,
214-
spanId: expect.any(String),
215-
startTimestamp: expect.any(Number),
216-
tags: {},
217-
traceId: expect.any(String),
218-
data: {
219-
'sentry.op': 'op',
220-
'sentry.origin': 'manual',
221-
},
222-
});
223-
});
224-
225-
test('updateWithContext should completely change span properties', () => {
226-
const originalContext = {
227-
traceId: 'a',
228-
spanId: 'b',
229-
sampled: false,
230-
description: 'test',
231-
op: 'op',
232-
tags: {
233-
tag0: 'hello',
234-
},
235-
};
236-
const span = new SentrySpan(originalContext);
237-
238-
span.updateWithContext({
239-
traceId: 'c',
240-
spanId: 'd',
241-
sampled: true,
242-
});
243-
244-
expect(span.spanContext().traceId).toBe('c');
245-
expect(span.spanContext().spanId).toBe('d');
246-
expect(span.sampled).toBe(true);
247-
expect(spanToJSON(span).description).toBe(undefined);
248-
expect(spanToJSON(span).op).toBe(undefined);
249-
expect(span.tags).toStrictEqual({});
250-
});
251-
252-
test('using toContext and updateWithContext together should update only changed properties', () => {
253-
const originalContext = {
254-
traceId: 'a',
255-
spanId: 'b',
256-
sampled: false,
257-
description: 'test',
258-
op: 'op',
259-
tags: { tag0: 'hello' },
260-
data: { data0: 'foo' },
261-
};
262-
const span = new SentrySpan(originalContext);
263-
264-
const newContext = {
265-
...span.toContext(),
266-
description: 'new',
267-
endTimestamp: 1,
268-
op: 'new-op',
269-
sampled: true,
270-
tags: {
271-
tag1: 'bye',
272-
},
273-
data: {
274-
...span.toContext().data,
275-
},
276-
};
277-
278-
if (newContext.data) newContext.data.data1 = 'bar';
279-
280-
span.updateWithContext(newContext);
281-
282-
expect(span.spanContext().traceId).toBe('a');
283-
expect(span.spanContext().spanId).toBe('b');
284-
expect(spanToJSON(span).description).toBe('new');
285-
expect(spanToJSON(span).timestamp).toBe(1);
286-
expect(spanToJSON(span).op).toBe('new-op');
287-
expect(span.sampled).toBe(true);
288-
expect(span.tags).toStrictEqual({ tag1: 'bye' });
289-
expect(span.data).toStrictEqual({
290-
data0: 'foo',
291-
data1: 'bar',
292-
'sentry.op': 'op',
293-
'sentry.origin': 'manual',
294-
});
295-
});
296-
});
297-
298-
/* eslint-enable deprecation/deprecation */
299-
300123
describe('setAttribute', () => {
301124
it('allows to set attributes', () => {
302125
const span = new SentrySpan();

0 commit comments

Comments
 (0)