Skip to content

Commit 983e1c0

Browse files
committed
test(otel): Migrate to Vitest
1 parent f024ccf commit 983e1c0

25 files changed

+259
-211
lines changed

packages/opentelemetry/jest.config.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/opentelemetry/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@
7373
"clean": "rimraf build coverage sentry-opentelemetry-*.tgz",
7474
"fix": "eslint . --format stylish --fix",
7575
"lint": "eslint . --format stylish",
76-
"test": "yarn test:jest",
77-
"test:jest": "jest",
78-
"test:watch": "jest --watch",
76+
"test": "yarn test:unit",
77+
"test:unit": "vitest run",
78+
"test:watch": "vitest --watch",
7979
"yalc:publish": "yalc publish --push --sig"
8080
},
8181
"volta": {

packages/opentelemetry/test/asyncContextStrategy.test.ts

Lines changed: 123 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
withIsolationScope,
88
withScope,
99
} from '@sentry/core';
10+
import { describe, beforeEach, afterEach, afterAll, test, expect, it } from 'vitest';
1011

1112
import type { Scope } from '@sentry/core';
1213
import { setOpenTelemetryContextAsyncContextStrategy } from '../src/asyncContextStrategy';
@@ -301,130 +302,142 @@ describe('asyncContextStrategy', () => {
301302
});
302303

303304
describe('withScope()', () => {
304-
it('will make the passed scope the active scope within the callback', done => {
305-
withScope(scope => {
306-
expect(getCurrentScope()).toBe(scope);
307-
done();
308-
});
309-
});
310-
311-
it('will pass a scope that is different from the current active isolation scope', done => {
312-
withScope(scope => {
313-
expect(getIsolationScope()).not.toBe(scope);
314-
done();
315-
});
316-
});
317-
318-
it('will always make the inner most passed scope the current scope when nesting calls', done => {
319-
withIsolationScope(_scope1 => {
320-
withIsolationScope(scope2 => {
321-
expect(getIsolationScope()).toBe(scope2);
305+
it('will make the passed scope the active scope within the callback', () =>
306+
new Promise<void>(done => {
307+
withScope(scope => {
308+
expect(getCurrentScope()).toBe(scope);
322309
done();
323310
});
324-
});
325-
});
326-
327-
it('forks the scope when not passing any scope', done => {
328-
const initialScope = getCurrentScope();
329-
initialScope.setTag('aa', 'aa');
330-
331-
withScope(scope => {
332-
expect(getCurrentScope()).toBe(scope);
333-
scope.setTag('bb', 'bb');
334-
expect(scope).not.toBe(initialScope);
335-
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
336-
done();
337-
});
338-
});
339-
340-
it('forks the scope when passing undefined', done => {
341-
const initialScope = getCurrentScope();
342-
initialScope.setTag('aa', 'aa');
311+
}));
343312

344-
withScope(undefined, scope => {
345-
expect(getCurrentScope()).toBe(scope);
346-
scope.setTag('bb', 'bb');
347-
expect(scope).not.toBe(initialScope);
348-
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
349-
done();
350-
});
351-
});
313+
it('will pass a scope that is different from the current active isolation scope', () =>
314+
new Promise<void>(done => {
315+
withScope(scope => {
316+
expect(getIsolationScope()).not.toBe(scope);
317+
done();
318+
});
319+
}));
320+
321+
it('will always make the inner most passed scope the current scope when nesting calls', () =>
322+
new Promise<void>(done => {
323+
withIsolationScope(_scope1 => {
324+
withIsolationScope(scope2 => {
325+
expect(getIsolationScope()).toBe(scope2);
326+
done();
327+
});
328+
});
329+
}));
330+
331+
it('forks the scope when not passing any scope', () =>
332+
new Promise<void>(done => {
333+
const initialScope = getCurrentScope();
334+
initialScope.setTag('aa', 'aa');
335+
336+
withScope(scope => {
337+
expect(getCurrentScope()).toBe(scope);
338+
scope.setTag('bb', 'bb');
339+
expect(scope).not.toBe(initialScope);
340+
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
341+
done();
342+
});
343+
}));
344+
345+
it('forks the scope when passing undefined', () =>
346+
new Promise<void>(done => {
347+
const initialScope = getCurrentScope();
348+
initialScope.setTag('aa', 'aa');
349+
350+
withScope(undefined, scope => {
351+
expect(getCurrentScope()).toBe(scope);
352+
scope.setTag('bb', 'bb');
353+
expect(scope).not.toBe(initialScope);
354+
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
355+
done();
356+
});
357+
}));
352358

353-
it('sets the passed in scope as active scope', done => {
354-
const initialScope = getCurrentScope();
355-
initialScope.setTag('aa', 'aa');
359+
it('sets the passed in scope as active scope', () =>
360+
new Promise<void>(done => {
361+
const initialScope = getCurrentScope();
362+
initialScope.setTag('aa', 'aa');
356363

357-
const customScope = new ScopeClass();
364+
const customScope = new ScopeClass();
358365

359-
withScope(customScope, scope => {
360-
expect(getCurrentScope()).toBe(customScope);
361-
expect(scope).toBe(customScope);
362-
done();
363-
});
364-
});
366+
withScope(customScope, scope => {
367+
expect(getCurrentScope()).toBe(customScope);
368+
expect(scope).toBe(customScope);
369+
done();
370+
});
371+
}));
365372
});
366373

367374
describe('withIsolationScope()', () => {
368-
it('will make the passed isolation scope the active isolation scope within the callback', done => {
369-
withIsolationScope(scope => {
370-
expect(getIsolationScope()).toBe(scope);
371-
done();
372-
});
373-
});
374-
375-
it('will pass an isolation scope that is different from the current active scope', done => {
376-
withIsolationScope(scope => {
377-
expect(getCurrentScope()).not.toBe(scope);
378-
done();
379-
});
380-
});
381-
382-
it('will always make the inner most passed scope the current scope when nesting calls', done => {
383-
withIsolationScope(_scope1 => {
384-
withIsolationScope(scope2 => {
385-
expect(getIsolationScope()).toBe(scope2);
375+
it('will make the passed isolation scope the active isolation scope within the callback', () =>
376+
new Promise<void>(done => {
377+
withIsolationScope(scope => {
378+
expect(getIsolationScope()).toBe(scope);
386379
done();
387380
});
388-
});
389-
});
390-
391-
it('forks the isolation scope when not passing any isolation scope', done => {
392-
const initialScope = getIsolationScope();
393-
initialScope.setTag('aa', 'aa');
394-
395-
withIsolationScope(scope => {
396-
expect(getIsolationScope()).toBe(scope);
397-
scope.setTag('bb', 'bb');
398-
expect(scope).not.toBe(initialScope);
399-
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
400-
done();
401-
});
402-
});
403-
404-
it('forks the isolation scope when passing undefined', done => {
405-
const initialScope = getIsolationScope();
406-
initialScope.setTag('aa', 'aa');
381+
}));
407382

408-
withIsolationScope(undefined, scope => {
409-
expect(getIsolationScope()).toBe(scope);
410-
scope.setTag('bb', 'bb');
411-
expect(scope).not.toBe(initialScope);
412-
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
413-
done();
414-
});
415-
});
383+
it('will pass an isolation scope that is different from the current active scope', () =>
384+
new Promise<void>(done => {
385+
withIsolationScope(scope => {
386+
expect(getCurrentScope()).not.toBe(scope);
387+
done();
388+
});
389+
}));
390+
391+
it('will always make the inner most passed scope the current scope when nesting calls', () =>
392+
new Promise<void>(done => {
393+
withIsolationScope(_scope1 => {
394+
withIsolationScope(scope2 => {
395+
expect(getIsolationScope()).toBe(scope2);
396+
done();
397+
});
398+
});
399+
}));
400+
401+
it('forks the isolation scope when not passing any isolation scope', () =>
402+
new Promise<void>(done => {
403+
const initialScope = getIsolationScope();
404+
initialScope.setTag('aa', 'aa');
405+
406+
withIsolationScope(scope => {
407+
expect(getIsolationScope()).toBe(scope);
408+
scope.setTag('bb', 'bb');
409+
expect(scope).not.toBe(initialScope);
410+
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
411+
done();
412+
});
413+
}));
414+
415+
it('forks the isolation scope when passing undefined', () =>
416+
new Promise<void>(done => {
417+
const initialScope = getIsolationScope();
418+
initialScope.setTag('aa', 'aa');
419+
420+
withIsolationScope(undefined, scope => {
421+
expect(getIsolationScope()).toBe(scope);
422+
scope.setTag('bb', 'bb');
423+
expect(scope).not.toBe(initialScope);
424+
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
425+
done();
426+
});
427+
}));
416428

417-
it('sets the passed in isolation scope as active isolation scope', done => {
418-
const initialScope = getIsolationScope();
419-
initialScope.setTag('aa', 'aa');
429+
it('sets the passed in isolation scope as active isolation scope', () =>
430+
new Promise<void>(done => {
431+
const initialScope = getIsolationScope();
432+
initialScope.setTag('aa', 'aa');
420433

421-
const customScope = new ScopeClass();
434+
const customScope = new ScopeClass();
422435

423-
withIsolationScope(customScope, scope => {
424-
expect(getIsolationScope()).toBe(customScope);
425-
expect(scope).toBe(customScope);
426-
done();
427-
});
428-
});
436+
withIsolationScope(customScope, scope => {
437+
expect(getIsolationScope()).toBe(customScope);
438+
expect(scope).toBe(customScope);
439+
done();
440+
});
441+
}));
429442
});
430443
});

packages/opentelemetry/test/custom/client.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ProxyTracer } from '@opentelemetry/api';
2+
import { describe, expect, it } from 'vitest';
23

34
import { TestClient, getDefaultTestClientOptions } from '../helpers/TestClient';
45

packages/opentelemetry/test/helpers/mockSdkInit.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { ProxyTracerProvider, context, propagation, trace } from '@opentelemetry/api';
22
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
33
import type { ClientOptions, Options } from '@sentry/core';
4+
import { getClient } from '@sentry/core';
45

5-
import { getCurrentScope, getGlobalScope, getIsolationScope } from '@sentry/core';
6+
import { getCurrentScope, getGlobalScope, getIsolationScope, flush } from '@sentry/core';
67
import { setOpenTelemetryContextAsyncContextStrategy } from '../../src/asyncContextStrategy';
78
import { clearOpenTelemetrySetupCheck } from '../../src/utils/setupCheck';
89
import { init as initTestClient } from './TestClient';
910
import { initOtel } from './initOtel';
11+
import type { OpenTelemetryClient } from '../../src/types';
1012

1113
const PUBLIC_DSN = 'https://username@domain/123';
1214

@@ -24,6 +26,7 @@ function resetGlobals(): void {
2426
getCurrentScope().setClient(undefined);
2527
getIsolationScope().clear();
2628
getGlobalScope().clear();
29+
delete (global as any).__SENTRY__;
2730
}
2831

2932
export function mockSdkInit(options?: Partial<ClientOptions>) {
@@ -32,25 +35,26 @@ export function mockSdkInit(options?: Partial<ClientOptions>) {
3235
init({ dsn: PUBLIC_DSN, ...options });
3336
}
3437

35-
export function cleanupOtel(_provider?: BasicTracerProvider): void {
38+
export async function cleanupOtel(_provider?: BasicTracerProvider): Promise<void> {
3639
clearOpenTelemetrySetupCheck();
40+
3741
const provider = getProvider(_provider);
3842

39-
if (!provider) {
40-
return;
43+
if (provider) {
44+
await provider.forceFlush();
45+
await provider.shutdown();
4146
}
4247

43-
void provider.forceFlush();
44-
void provider.shutdown();
45-
4648
// Disable all globally registered APIs
4749
trace.disable();
4850
context.disable();
4951
propagation.disable();
52+
53+
await flush();
5054
}
5155

5256
export function getProvider(_provider?: BasicTracerProvider): BasicTracerProvider | undefined {
53-
let provider = _provider || trace.getTracerProvider();
57+
let provider = _provider || getClient<OpenTelemetryClient>()?.traceProvider || trace.getTracerProvider();
5458

5559
if (provider instanceof ProxyTracerProvider) {
5660
provider = provider.getDelegate();

0 commit comments

Comments
 (0)