Skip to content

Commit 80d76c9

Browse files
authored
test(vercel-edge): Switch to using vitest (#12958)
Before: `Time: 2.621 s` After: `Duration 638ms (transform 343ms, setup 0ms, collect 1.61s, tests 22ms, environment 0ms, prepare 278ms)` ref #11084 Also removes `edge-runtime/jest-environment` which we weren't using anyway, removes a lot of unnecessary stuff in our `yarn.lock` (-151 lines!)
1 parent 5ceb1f4 commit 80d76c9

File tree

9 files changed

+164
-281
lines changed

9 files changed

+164
-281
lines changed

packages/vercel-edge/jest.config.js

-7
This file was deleted.

packages/vercel-edge/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
"@sentry/utils": "8.18.0"
4545
},
4646
"devDependencies": {
47-
"@edge-runtime/jest-environment": "2.2.3",
48-
"@edge-runtime/types": "2.2.3"
47+
"@edge-runtime/types": "3.0.1"
4948
},
5049
"scripts": {
5150
"build": "run-p build:transpile build:types",
@@ -63,8 +62,8 @@
6362
"clean": "rimraf build coverage sentry-vercel-edge-*.tgz",
6463
"fix": "eslint . --format stylish --fix",
6564
"lint": "eslint . --format stylish",
66-
"test": "jest",
67-
"test:watch": "jest --watch",
65+
"test": "vitest run",
66+
"test:watch": "vitest --watch",
6867
"yalc:publish": "yalc publish --push --sig"
6968
},
7069
"volta": {
+123-110
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Scope, getCurrentScope, getGlobalScope, getIsolationScope, withIsolationScope, withScope } from '@sentry/core';
22
import { GLOBAL_OBJ } from '@sentry/utils';
33
import { AsyncLocalStorage } from 'async_hooks';
4+
import { beforeEach, describe, expect, it } from 'vitest';
45
import { setAsyncLocalStorageAsyncContextStrategy } from '../src/async';
56

67
describe('withScope()', () => {
@@ -13,67 +14,73 @@ describe('withScope()', () => {
1314
setAsyncLocalStorageAsyncContextStrategy();
1415
});
1516

16-
it('will make the passed scope the active scope within the callback', done => {
17-
withScope(scope => {
18-
expect(getCurrentScope()).toBe(scope);
19-
done();
20-
});
21-
});
22-
23-
it('will pass a scope that is different from the current active isolation scope', done => {
24-
withScope(scope => {
25-
expect(getIsolationScope()).not.toBe(scope);
26-
done();
27-
});
28-
});
29-
30-
it('will always make the inner most passed scope the current scope when nesting calls', done => {
31-
withIsolationScope(_scope1 => {
32-
withIsolationScope(scope2 => {
33-
expect(getIsolationScope()).toBe(scope2);
17+
it('will make the passed scope the active scope within the callback', () =>
18+
new Promise<void>(done => {
19+
withScope(scope => {
20+
expect(getCurrentScope()).toBe(scope);
3421
done();
3522
});
36-
});
37-
});
23+
}));
3824

39-
it('forks the scope when not passing any scope', done => {
40-
const initialScope = getCurrentScope();
41-
initialScope.setTag('aa', 'aa');
42-
43-
withScope(scope => {
44-
expect(getCurrentScope()).toBe(scope);
45-
scope.setTag('bb', 'bb');
46-
expect(scope).not.toBe(initialScope);
47-
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
48-
done();
49-
});
50-
});
51-
52-
it('forks the scope when passing undefined', done => {
53-
const initialScope = getCurrentScope();
54-
initialScope.setTag('aa', 'aa');
55-
56-
withScope(undefined, scope => {
57-
expect(getCurrentScope()).toBe(scope);
58-
scope.setTag('bb', 'bb');
59-
expect(scope).not.toBe(initialScope);
60-
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
61-
done();
62-
});
63-
});
25+
it('will pass a scope that is different from the current active isolation scope', () =>
26+
new Promise<void>(done => {
27+
withScope(scope => {
28+
expect(getIsolationScope()).not.toBe(scope);
29+
done();
30+
});
31+
}));
32+
33+
it('will always make the inner most passed scope the current scope when nesting calls', () =>
34+
new Promise<void>(done => {
35+
withIsolationScope(_scope1 => {
36+
withIsolationScope(scope2 => {
37+
expect(getIsolationScope()).toBe(scope2);
38+
done();
39+
});
40+
});
41+
}));
42+
43+
it('forks the scope when not passing any scope', () =>
44+
new Promise<void>(done => {
45+
const initialScope = getCurrentScope();
46+
initialScope.setTag('aa', 'aa');
47+
48+
withScope(scope => {
49+
expect(getCurrentScope()).toBe(scope);
50+
scope.setTag('bb', 'bb');
51+
expect(scope).not.toBe(initialScope);
52+
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
53+
done();
54+
});
55+
}));
56+
57+
it('forks the scope when passing undefined', () =>
58+
new Promise<void>(done => {
59+
const initialScope = getCurrentScope();
60+
initialScope.setTag('aa', 'aa');
61+
62+
withScope(undefined, scope => {
63+
expect(getCurrentScope()).toBe(scope);
64+
scope.setTag('bb', 'bb');
65+
expect(scope).not.toBe(initialScope);
66+
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
67+
done();
68+
});
69+
}));
6470

65-
it('sets the passed in scope as active scope', done => {
66-
const initialScope = getCurrentScope();
67-
initialScope.setTag('aa', 'aa');
71+
it('sets the passed in scope as active scope', () =>
72+
new Promise<void>(done => {
73+
const initialScope = getCurrentScope();
74+
initialScope.setTag('aa', 'aa');
6875

69-
const customScope = new Scope();
76+
const customScope = new Scope();
7077

71-
withScope(customScope, scope => {
72-
expect(getCurrentScope()).toBe(customScope);
73-
expect(scope).toBe(customScope);
74-
done();
75-
});
76-
});
78+
withScope(customScope, scope => {
79+
expect(getCurrentScope()).toBe(customScope);
80+
expect(scope).toBe(customScope);
81+
done();
82+
});
83+
}));
7784
});
7885

7986
describe('withIsolationScope()', () => {
@@ -86,65 +93,71 @@ describe('withIsolationScope()', () => {
8693
setAsyncLocalStorageAsyncContextStrategy();
8794
});
8895

89-
it('will make the passed isolation scope the active isolation scope within the callback', done => {
90-
withIsolationScope(scope => {
91-
expect(getIsolationScope()).toBe(scope);
92-
done();
93-
});
94-
});
95-
96-
it('will pass an isolation scope that is different from the current active scope', done => {
97-
withIsolationScope(scope => {
98-
expect(getCurrentScope()).not.toBe(scope);
99-
done();
100-
});
101-
});
102-
103-
it('will always make the inner most passed scope the current scope when nesting calls', done => {
104-
withIsolationScope(_scope1 => {
105-
withIsolationScope(scope2 => {
106-
expect(getIsolationScope()).toBe(scope2);
96+
it('will make the passed isolation scope the active isolation scope within the callback', () =>
97+
new Promise<void>(done => {
98+
withIsolationScope(scope => {
99+
expect(getIsolationScope()).toBe(scope);
107100
done();
108101
});
109-
});
110-
});
102+
}));
111103

112-
it('forks the isolation scope when not passing any isolation scope', done => {
113-
const initialScope = getIsolationScope();
114-
initialScope.setTag('aa', 'aa');
115-
116-
withIsolationScope(scope => {
117-
expect(getIsolationScope()).toBe(scope);
118-
scope.setTag('bb', 'bb');
119-
expect(scope).not.toBe(initialScope);
120-
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
121-
done();
122-
});
123-
});
124-
125-
it('forks the isolation scope when passing undefined', done => {
126-
const initialScope = getIsolationScope();
127-
initialScope.setTag('aa', 'aa');
128-
129-
withIsolationScope(undefined, scope => {
130-
expect(getIsolationScope()).toBe(scope);
131-
scope.setTag('bb', 'bb');
132-
expect(scope).not.toBe(initialScope);
133-
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
134-
done();
135-
});
136-
});
104+
it('will pass an isolation scope that is different from the current active scope', () =>
105+
new Promise<void>(done => {
106+
withIsolationScope(scope => {
107+
expect(getCurrentScope()).not.toBe(scope);
108+
done();
109+
});
110+
}));
111+
112+
it('will always make the inner most passed scope the current scope when nesting calls', () =>
113+
new Promise<void>(done => {
114+
withIsolationScope(_scope1 => {
115+
withIsolationScope(scope2 => {
116+
expect(getIsolationScope()).toBe(scope2);
117+
done();
118+
});
119+
});
120+
}));
121+
122+
it('forks the isolation scope when not passing any isolation scope', () =>
123+
new Promise<void>(done => {
124+
const initialScope = getIsolationScope();
125+
initialScope.setTag('aa', 'aa');
126+
127+
withIsolationScope(scope => {
128+
expect(getIsolationScope()).toBe(scope);
129+
scope.setTag('bb', 'bb');
130+
expect(scope).not.toBe(initialScope);
131+
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
132+
done();
133+
});
134+
}));
135+
136+
it('forks the isolation scope when passing undefined', () =>
137+
new Promise<void>(done => {
138+
const initialScope = getIsolationScope();
139+
initialScope.setTag('aa', 'aa');
140+
141+
withIsolationScope(undefined, scope => {
142+
expect(getIsolationScope()).toBe(scope);
143+
scope.setTag('bb', 'bb');
144+
expect(scope).not.toBe(initialScope);
145+
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
146+
done();
147+
});
148+
}));
137149

138-
it('sets the passed in isolation scope as active isolation scope', done => {
139-
const initialScope = getIsolationScope();
140-
initialScope.setTag('aa', 'aa');
150+
it('sets the passed in isolation scope as active isolation scope', () =>
151+
new Promise<void>(done => {
152+
const initialScope = getIsolationScope();
153+
initialScope.setTag('aa', 'aa');
141154

142-
const customScope = new Scope();
155+
const customScope = new Scope();
143156

144-
withIsolationScope(customScope, scope => {
145-
expect(getIsolationScope()).toBe(customScope);
146-
expect(scope).toBe(customScope);
147-
done();
148-
});
149-
});
157+
withIsolationScope(customScope, scope => {
158+
expect(getIsolationScope()).toBe(customScope);
159+
expect(scope).toBe(customScope);
160+
done();
161+
});
162+
}));
150163
});

packages/vercel-edge/test/sdk.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
13
import * as SentryCore from '@sentry/core';
24
import { init } from '../src/sdk';
35

46
describe('init', () => {
57
it('initializes and returns client', () => {
6-
const initSpy = jest.spyOn(SentryCore, 'initAndBind');
8+
const initSpy = vi.spyOn(SentryCore, 'initAndBind');
79

810
expect(init({})).not.toBeUndefined();
911
expect(initSpy).toHaveBeenCalledTimes(1);

packages/vercel-edge/test/transports/index.test.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { afterAll, describe, expect, it, vi } from 'vitest';
2+
13
import type { EventEnvelope, EventItem } from '@sentry/types';
24
import { createEnvelope, serializeEnvelope } from '@sentry/utils';
35

@@ -23,7 +25,7 @@ class Headers {
2325
}
2426
}
2527

26-
const mockFetch = jest.fn();
28+
const mockFetch = vi.fn();
2729

2830
const oldFetch = global.fetch;
2931
global.fetch = mockFetch;
@@ -57,7 +59,7 @@ describe('Edge Transport', () => {
5759

5860
it('sets rate limit headers', async () => {
5961
const headers = {
60-
get: jest.fn(),
62+
get: vi.fn(),
6163
};
6264

6365
mockFetch.mockImplementationOnce(() =>
@@ -110,8 +112,8 @@ describe('IsolatedPromiseBuffer', () => {
110112
it('should not call tasks until drained', async () => {
111113
const ipb = new IsolatedPromiseBuffer();
112114

113-
const task1 = jest.fn(() => Promise.resolve({}));
114-
const task2 = jest.fn(() => Promise.resolve({}));
115+
const task1 = vi.fn(() => Promise.resolve({}));
116+
const task2 = vi.fn(() => Promise.resolve({}));
115117

116118
await ipb.add(task1);
117119
await ipb.add(task2);
@@ -128,10 +130,10 @@ describe('IsolatedPromiseBuffer', () => {
128130
it('should not allow adding more items than the specified limit', async () => {
129131
const ipb = new IsolatedPromiseBuffer(3);
130132

131-
const task1 = jest.fn(() => Promise.resolve({}));
132-
const task2 = jest.fn(() => Promise.resolve({}));
133-
const task3 = jest.fn(() => Promise.resolve({}));
134-
const task4 = jest.fn(() => Promise.resolve({}));
133+
const task1 = vi.fn(() => Promise.resolve({}));
134+
const task2 = vi.fn(() => Promise.resolve({}));
135+
const task3 = vi.fn(() => Promise.resolve({}));
136+
const task4 = vi.fn(() => Promise.resolve({}));
135137

136138
await ipb.add(task1);
137139
await ipb.add(task2);
@@ -143,8 +145,8 @@ describe('IsolatedPromiseBuffer', () => {
143145
it('should not throw when one of the tasks throws when drained', async () => {
144146
const ipb = new IsolatedPromiseBuffer();
145147

146-
const task1 = jest.fn(() => Promise.resolve({}));
147-
const task2 = jest.fn(() => Promise.reject(new Error()));
148+
const task1 = vi.fn(() => Promise.resolve({}));
149+
const task2 = vi.fn(() => Promise.reject(new Error()));
148150

149151
await ipb.add(task1);
150152
await ipb.add(task2);

0 commit comments

Comments
 (0)