Skip to content

test(vercel-edge): Switch to using vitest #12958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions packages/vercel-edge/jest.config.js

This file was deleted.

7 changes: 3 additions & 4 deletions packages/vercel-edge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
"@sentry/utils": "8.18.0"
},
"devDependencies": {
"@edge-runtime/jest-environment": "2.2.3",
"@edge-runtime/types": "2.2.3"
"@edge-runtime/types": "3.0.1"
},
"scripts": {
"build": "run-p build:transpile build:types",
Expand All @@ -63,8 +62,8 @@
"clean": "rimraf build coverage sentry-vercel-edge-*.tgz",
"fix": "eslint . --format stylish --fix",
"lint": "eslint . --format stylish",
"test": "jest",
"test:watch": "jest --watch",
"test": "vitest run",
"test:watch": "vitest --watch",
"yalc:publish": "yalc publish --push --sig"
},
"volta": {
Expand Down
233 changes: 123 additions & 110 deletions packages/vercel-edge/test/async.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Scope, getCurrentScope, getGlobalScope, getIsolationScope, withIsolationScope, withScope } from '@sentry/core';
import { GLOBAL_OBJ } from '@sentry/utils';
import { AsyncLocalStorage } from 'async_hooks';
import { beforeEach, describe, expect, it } from 'vitest';
import { setAsyncLocalStorageAsyncContextStrategy } from '../src/async';

describe('withScope()', () => {
Expand All @@ -13,67 +14,73 @@ describe('withScope()', () => {
setAsyncLocalStorageAsyncContextStrategy();
});

it('will make the passed scope the active scope within the callback', done => {
withScope(scope => {
expect(getCurrentScope()).toBe(scope);
done();
});
});

it('will pass a scope that is different from the current active isolation scope', done => {
withScope(scope => {
expect(getIsolationScope()).not.toBe(scope);
done();
});
});

it('will always make the inner most passed scope the current scope when nesting calls', done => {
withIsolationScope(_scope1 => {
withIsolationScope(scope2 => {
expect(getIsolationScope()).toBe(scope2);
it('will make the passed scope the active scope within the callback', () =>
new Promise<void>(done => {
withScope(scope => {
expect(getCurrentScope()).toBe(scope);
done();
});
});
});
}));

it('forks the scope when not passing any scope', done => {
const initialScope = getCurrentScope();
initialScope.setTag('aa', 'aa');

withScope(scope => {
expect(getCurrentScope()).toBe(scope);
scope.setTag('bb', 'bb');
expect(scope).not.toBe(initialScope);
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
done();
});
});

it('forks the scope when passing undefined', done => {
const initialScope = getCurrentScope();
initialScope.setTag('aa', 'aa');

withScope(undefined, scope => {
expect(getCurrentScope()).toBe(scope);
scope.setTag('bb', 'bb');
expect(scope).not.toBe(initialScope);
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
done();
});
});
it('will pass a scope that is different from the current active isolation scope', () =>
new Promise<void>(done => {
withScope(scope => {
expect(getIsolationScope()).not.toBe(scope);
done();
});
}));

it('will always make the inner most passed scope the current scope when nesting calls', () =>
new Promise<void>(done => {
withIsolationScope(_scope1 => {
withIsolationScope(scope2 => {
expect(getIsolationScope()).toBe(scope2);
done();
});
});
}));

it('forks the scope when not passing any scope', () =>
new Promise<void>(done => {
const initialScope = getCurrentScope();
initialScope.setTag('aa', 'aa');

withScope(scope => {
expect(getCurrentScope()).toBe(scope);
scope.setTag('bb', 'bb');
expect(scope).not.toBe(initialScope);
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
done();
});
}));

it('forks the scope when passing undefined', () =>
new Promise<void>(done => {
const initialScope = getCurrentScope();
initialScope.setTag('aa', 'aa');

withScope(undefined, scope => {
expect(getCurrentScope()).toBe(scope);
scope.setTag('bb', 'bb');
expect(scope).not.toBe(initialScope);
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
done();
});
}));

it('sets the passed in scope as active scope', done => {
const initialScope = getCurrentScope();
initialScope.setTag('aa', 'aa');
it('sets the passed in scope as active scope', () =>
new Promise<void>(done => {
const initialScope = getCurrentScope();
initialScope.setTag('aa', 'aa');

const customScope = new Scope();
const customScope = new Scope();

withScope(customScope, scope => {
expect(getCurrentScope()).toBe(customScope);
expect(scope).toBe(customScope);
done();
});
});
withScope(customScope, scope => {
expect(getCurrentScope()).toBe(customScope);
expect(scope).toBe(customScope);
done();
});
}));
});

describe('withIsolationScope()', () => {
Expand All @@ -86,65 +93,71 @@ describe('withIsolationScope()', () => {
setAsyncLocalStorageAsyncContextStrategy();
});

it('will make the passed isolation scope the active isolation scope within the callback', done => {
withIsolationScope(scope => {
expect(getIsolationScope()).toBe(scope);
done();
});
});

it('will pass an isolation scope that is different from the current active scope', done => {
withIsolationScope(scope => {
expect(getCurrentScope()).not.toBe(scope);
done();
});
});

it('will always make the inner most passed scope the current scope when nesting calls', done => {
withIsolationScope(_scope1 => {
withIsolationScope(scope2 => {
expect(getIsolationScope()).toBe(scope2);
it('will make the passed isolation scope the active isolation scope within the callback', () =>
new Promise<void>(done => {
withIsolationScope(scope => {
expect(getIsolationScope()).toBe(scope);
done();
});
});
});
}));

it('forks the isolation scope when not passing any isolation scope', done => {
const initialScope = getIsolationScope();
initialScope.setTag('aa', 'aa');

withIsolationScope(scope => {
expect(getIsolationScope()).toBe(scope);
scope.setTag('bb', 'bb');
expect(scope).not.toBe(initialScope);
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
done();
});
});

it('forks the isolation scope when passing undefined', done => {
const initialScope = getIsolationScope();
initialScope.setTag('aa', 'aa');

withIsolationScope(undefined, scope => {
expect(getIsolationScope()).toBe(scope);
scope.setTag('bb', 'bb');
expect(scope).not.toBe(initialScope);
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
done();
});
});
it('will pass an isolation scope that is different from the current active scope', () =>
new Promise<void>(done => {
withIsolationScope(scope => {
expect(getCurrentScope()).not.toBe(scope);
done();
});
}));

it('will always make the inner most passed scope the current scope when nesting calls', () =>
new Promise<void>(done => {
withIsolationScope(_scope1 => {
withIsolationScope(scope2 => {
expect(getIsolationScope()).toBe(scope2);
done();
});
});
}));

it('forks the isolation scope when not passing any isolation scope', () =>
new Promise<void>(done => {
const initialScope = getIsolationScope();
initialScope.setTag('aa', 'aa');

withIsolationScope(scope => {
expect(getIsolationScope()).toBe(scope);
scope.setTag('bb', 'bb');
expect(scope).not.toBe(initialScope);
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
done();
});
}));

it('forks the isolation scope when passing undefined', () =>
new Promise<void>(done => {
const initialScope = getIsolationScope();
initialScope.setTag('aa', 'aa');

withIsolationScope(undefined, scope => {
expect(getIsolationScope()).toBe(scope);
scope.setTag('bb', 'bb');
expect(scope).not.toBe(initialScope);
expect(scope.getScopeData().tags).toEqual({ aa: 'aa', bb: 'bb' });
done();
});
}));

it('sets the passed in isolation scope as active isolation scope', done => {
const initialScope = getIsolationScope();
initialScope.setTag('aa', 'aa');
it('sets the passed in isolation scope as active isolation scope', () =>
new Promise<void>(done => {
const initialScope = getIsolationScope();
initialScope.setTag('aa', 'aa');

const customScope = new Scope();
const customScope = new Scope();

withIsolationScope(customScope, scope => {
expect(getIsolationScope()).toBe(customScope);
expect(scope).toBe(customScope);
done();
});
});
withIsolationScope(customScope, scope => {
expect(getIsolationScope()).toBe(customScope);
expect(scope).toBe(customScope);
done();
});
}));
});
4 changes: 3 additions & 1 deletion packages/vercel-edge/test/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { describe, expect, it, vi } from 'vitest';

import * as SentryCore from '@sentry/core';
import { init } from '../src/sdk';

describe('init', () => {
it('initializes and returns client', () => {
const initSpy = jest.spyOn(SentryCore, 'initAndBind');
const initSpy = vi.spyOn(SentryCore, 'initAndBind');

expect(init({})).not.toBeUndefined();
expect(initSpy).toHaveBeenCalledTimes(1);
Expand Down
22 changes: 12 additions & 10 deletions packages/vercel-edge/test/transports/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { afterAll, describe, expect, it, vi } from 'vitest';

import type { EventEnvelope, EventItem } from '@sentry/types';
import { createEnvelope, serializeEnvelope } from '@sentry/utils';

Expand All @@ -23,7 +25,7 @@ class Headers {
}
}

const mockFetch = jest.fn();
const mockFetch = vi.fn();

const oldFetch = global.fetch;
global.fetch = mockFetch;
Expand Down Expand Up @@ -57,7 +59,7 @@ describe('Edge Transport', () => {

it('sets rate limit headers', async () => {
const headers = {
get: jest.fn(),
get: vi.fn(),
};

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

const task1 = jest.fn(() => Promise.resolve({}));
const task2 = jest.fn(() => Promise.resolve({}));
const task1 = vi.fn(() => Promise.resolve({}));
const task2 = vi.fn(() => Promise.resolve({}));

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

const task1 = jest.fn(() => Promise.resolve({}));
const task2 = jest.fn(() => Promise.resolve({}));
const task3 = jest.fn(() => Promise.resolve({}));
const task4 = jest.fn(() => Promise.resolve({}));
const task1 = vi.fn(() => Promise.resolve({}));
const task2 = vi.fn(() => Promise.resolve({}));
const task3 = vi.fn(() => Promise.resolve({}));
const task4 = vi.fn(() => Promise.resolve({}));

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

const task1 = jest.fn(() => Promise.resolve({}));
const task2 = jest.fn(() => Promise.reject(new Error()));
const task1 = vi.fn(() => Promise.resolve({}));
const task2 = vi.fn(() => Promise.reject(new Error()));

await ipb.add(task1);
await ipb.add(task2);
Expand Down
Loading
Loading