Skip to content

test(google-serverless): Migrate to Vitest #15567

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 8 commits into from
Mar 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/google-cloud-serverless/jest.config.js

This file was deleted.

7 changes: 2 additions & 5 deletions packages/google-cloud-serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@
"devDependencies": {
"@google-cloud/bigquery": "^5.3.0",
"@google-cloud/common": "^3.4.1",
"@google-cloud/functions-framework": "^1.7.1",
"@google-cloud/pubsub": "^2.5.0",
"@types/node": "^18.19.1",
"google-gax": "^2.9.0",
"nock": "^13.5.5"
},
"scripts": {
Expand All @@ -77,8 +74,8 @@
"clean": "rimraf build coverage sentry-google-cloud-*.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
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { Client, IntegrationFn } from '@sentry/core';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration, fill, getClient } from '@sentry/core';
import { startInactiveSpan } from '@sentry/node';

interface GrpcFunction extends CallableFunction {
export interface GrpcFunction extends CallableFunction {
(...args: unknown[]): EventEmitter;
}

interface GrpcFunctionObject extends GrpcFunction {
export interface GrpcFunctionObject extends GrpcFunction {
requestStream: boolean;
responseStream: boolean;
originalName: string;
Expand All @@ -21,7 +21,7 @@ interface CreateStubFunc extends CallableFunction {
(createStub: unknown, options: StubOptions): PromiseLike<Stub>;
}

interface Stub {
export interface Stub {
[key: string]: GrpcFunctionObject;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ function wrapCreateStub(origCreate: CreateStubFunc): CreateStubFunc {
}

/** Patches the function in grpc stub to enable tracing */
function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: string): void {
export function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: string): void {
const funcObj = stub[methodName];
if (typeof funcObj !== 'function') {
return;
Expand Down
2 changes: 0 additions & 2 deletions packages/google-cloud-serverless/test/__mocks__/dns.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
import { describe, vi, beforeEach, test, expect } from 'vitest';

import { wrapCloudEventFunction } from '../../src/gcpfunction/cloud_events';
import type { CloudEventFunction, CloudEventFunctionWithCallback } from '../../src/gcpfunction/general';

const mockStartSpanManual = jest.fn((...spanArgs) => ({ ...spanArgs }));
const mockFlush = jest.fn((...args) => Promise.resolve(args));
const mockCaptureException = jest.fn();
const mockStartSpanManual = vi.fn((...spanArgs) => ({ ...spanArgs }));
const mockFlush = vi.fn((...args) => Promise.resolve(args));
const mockCaptureException = vi.fn();

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

const mockSpan = {
end: jest.fn(),
end: 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,
startSpanManual: (...args: unknown[]) => {
Expand All @@ -38,7 +40,7 @@ jest.mock('@sentry/node', () => {

describe('wrapCloudEventFunction', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

function handleCloudEvent(fn: CloudEventFunctionWithCallback): Promise<any> {
Expand Down
20 changes: 11 additions & 9 deletions packages/google-cloud-serverless/test/gcpfunction/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
import { describe, vi, beforeEach, test, expect } from 'vitest';

import type { Event } from '@sentry/core';
import { wrapEventFunction } from '../../src/gcpfunction/events';
import type { EventFunction, EventFunctionWithCallback } from '../../src/gcpfunction/general';

const mockStartSpanManual = jest.fn((...spanArgs) => ({ ...spanArgs }));
const mockFlush = jest.fn((...args) => Promise.resolve(args));
const mockCaptureException = jest.fn();
const mockStartSpanManual = vi.fn((...spanArgs) => ({ ...spanArgs }));
const mockFlush = vi.fn((...args) => Promise.resolve(args));
const mockCaptureException = vi.fn();

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

const mockSpan = {
end: jest.fn(),
end: 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,
startSpanManual: (...args: unknown[]) => {
Expand All @@ -39,7 +41,7 @@ jest.mock('@sentry/node', () => {

describe('wrapEventFunction', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

function handleEvent(fn: EventFunctionWithCallback): Promise<any> {
Expand Down Expand Up @@ -238,7 +240,7 @@ describe('wrapEventFunction', () => {
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
29 changes: 14 additions & 15 deletions packages/google-cloud-serverless/test/gcpfunction/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import type { Integration } from '@sentry/core';

import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
import { describe, vi, beforeEach, test, expect, type MockInstance } from 'vitest';

import { wrapHttpFunction } from '../../src/gcpfunction/http';

import type { HttpFunction, Request, Response } from '../../src/gcpfunction/general';

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

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

const mockScope = {
setSDKProcessingMetadata: jest.fn(),
setSDKProcessingMetadata: vi.fn(),
};

const mockSpan = {
end: jest.fn(),
end: 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,
init: (options: unknown) => {
Expand All @@ -47,7 +46,7 @@ jest.mock('@sentry/node', () => {

describe('GCPFunction', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

async function handleHttp(fn: HttpFunction, trace_headers: { [key: string]: string } | null = null): Promise<void> {
Expand Down Expand Up @@ -146,7 +145,7 @@ describe('GCPFunction', () => {
body: { foo: 'bar' },
} as Request;

const mockEnd = jest.fn();
const mockEnd = vi.fn();
const response = { end: mockEnd } as unknown as Response;

mockFlush.mockImplementationOnce(async () => {
Expand All @@ -171,8 +170,8 @@ describe('GCPFunction', () => {

await handleHttp(wrappedHandler);

const initOptions = (mockInit as unknown as jest.SpyInstance).mock.calls[0];
const defaultIntegrations = initOptions[0]?.defaultIntegrations.map((i: Integration) => i.name);
const initOptions = (mockInit as unknown as MockInstance).mock.calls[0];
const defaultIntegrations = initOptions?.[0]?.defaultIntegrations.map((i: Integration) => i.name);

expect(defaultIntegrations).toContain('RequestData');

Expand Down
Loading
Loading