Skip to content
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

test(aws-serverless): Migrate to Vitest #15487

Merged
merged 3 commits into from
Feb 25, 2025
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
1 change: 0 additions & 1 deletion packages/aws-serverless/jest.config.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/aws-serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
"clean": "rimraf build dist-awslambda-layer coverage sentry-serverless-*.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
6 changes: 4 additions & 2 deletions packages/aws-serverless/test/__mocks__/dns.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export const lookup = jest.fn();
export const resolveTxt = jest.fn();
import { vi } from 'vitest';

export const lookup = vi.fn();
export const resolveTxt = vi.fn();
34 changes: 18 additions & 16 deletions packages/aws-serverless/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } fr

import type { Event } from '@sentry/core';
import type { Callback, Handler } from 'aws-lambda';
import { beforeEach, describe, expect, test, vi } from 'vitest';

import { init, wrapHandler } from '../src/sdk';

const mockSpanEnd = jest.fn();
const mockStartInactiveSpan = jest.fn((...spanArgs) => ({ ...spanArgs }));
const mockStartSpanManual = jest.fn((...spanArgs) => ({ ...spanArgs }));
const mockFlush = jest.fn((...args) => Promise.resolve(args));
const mockWithScope = jest.fn();
const mockCaptureMessage = jest.fn();
const mockCaptureException = jest.fn();
const mockInit = jest.fn();
const mockSpanEnd = vi.fn();
const mockStartInactiveSpan = vi.fn((...spanArgs) => ({ ...spanArgs }));
const mockStartSpanManual = vi.fn((...spanArgs) => ({ ...spanArgs }));
const mockFlush = vi.fn((...args) => Promise.resolve(args));
const mockWithScope = vi.fn();
const mockCaptureMessage = vi.fn();
const mockCaptureException = vi.fn();
const mockInit = vi.fn();

const mockScope = {
setTag: jest.fn(),
setContext: jest.fn(),
addEventProcessor: jest.fn(),
setTransactionName: jest.fn(),
setTag: vi.fn(),
setContext: vi.fn(),
addEventProcessor: vi.fn(),
setTransactionName: vi.fn(),
};

jest.mock('@sentry/node', () => {
const original = jest.requireActual('@sentry/node');
vi.mock('@sentry/node', async () => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const original = (await vi.importActual('@sentry/node')) as typeof import('@sentry/node');
return {
...original,
initWithoutDefaultIntegrations: (options: unknown) => {
Expand Down Expand Up @@ -115,7 +117,7 @@ describe('AWSLambda', () => {
fortySix: 'o_O',
};

jest.clearAllMocks();
vi.clearAllMocks();
});

describe('wrapHandler() options', () => {
Expand Down Expand Up @@ -495,7 +497,7 @@ describe('AWSLambda', () => {
const scopeFunction = mockCaptureException.mock.calls[0][1];
const event: Event = { exception: { values: [{}] } };
let evtProcessor: ((e: Event) => Event) | undefined = undefined;
scopeFunction({ addEventProcessor: jest.fn().mockImplementation(proc => (evtProcessor = proc)) });
scopeFunction({ addEventProcessor: vi.fn().mockImplementation(proc => (evtProcessor = proc)) });

expect(evtProcessor).toBeInstanceOf(Function);
// @ts-expect-error just mocking around...
Expand Down
10 changes: 6 additions & 4 deletions packages/aws-serverless/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { afterEach, describe, expect, test, vi } from 'vitest';
import { eventContextExtractor, getAwsTraceData } from '../src/utils';

const mockExtractContext = jest.fn();
jest.mock('@opentelemetry/api', () => {
const actualApi = jest.requireActual('@opentelemetry/api');
const mockExtractContext = vi.fn();
vi.mock('@opentelemetry/api', async () => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const actualApi = (await vi.importActual('@opentelemetry/api')) as typeof import('@opentelemetry/api');
return {
...actualApi,
propagation: {
Expand Down Expand Up @@ -63,7 +65,7 @@ describe('getTraceData', () => {

describe('eventContextExtractor', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('passes sentry trace data to the propagation extractor', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-serverless/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"extends": "./tsconfig.json",

"include": ["test/**/*"],
"include": ["test/**/*", "vite.config.ts"],

"compilerOptions": {
// should include all types from `./tsconfig.json` plus types for all test frameworks used
"types": ["node", "jest"]
"types": ["node"]

// other package-specific, test-specific options
}
Expand Down
8 changes: 8 additions & 0 deletions packages/aws-serverless/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import baseConfig from '../../vite/vite.config';

export default {
...baseConfig,
test: {
...baseConfig.test,
},
};
Loading