Skip to content

Commit 12c5e73

Browse files
timfishAbhiPrasad
andauthored
test(google-serverless): Migrate to Vitest (#15567)
Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent 00a1018 commit 12c5e73

File tree

13 files changed

+345
-349
lines changed

13 files changed

+345
-349
lines changed

packages/google-cloud-serverless/jest.config.js

-1
This file was deleted.

packages/google-cloud-serverless/package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@
5555
"devDependencies": {
5656
"@google-cloud/bigquery": "^5.3.0",
5757
"@google-cloud/common": "^3.4.1",
58-
"@google-cloud/functions-framework": "^1.7.1",
59-
"@google-cloud/pubsub": "^2.5.0",
6058
"@types/node": "^18.19.1",
61-
"google-gax": "^2.9.0",
6259
"nock": "^13.5.5"
6360
},
6461
"scripts": {
@@ -77,8 +74,8 @@
7774
"clean": "rimraf build coverage sentry-google-cloud-*.tgz",
7875
"fix": "eslint . --format stylish --fix",
7976
"lint": "eslint . --format stylish",
80-
"test": "jest",
81-
"test:watch": "jest --watch",
77+
"test": "vitest run",
78+
"test:watch": "vitest --watch",
8279
"yalc:publish": "yalc publish --push --sig"
8380
},
8481
"volta": {

packages/google-cloud-serverless/src/integrations/google-cloud-grpc.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import type { Client, IntegrationFn } from '@sentry/core';
33
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration, fill, getClient } from '@sentry/core';
44
import { startInactiveSpan } from '@sentry/node';
55

6-
interface GrpcFunction extends CallableFunction {
6+
export interface GrpcFunction extends CallableFunction {
77
(...args: unknown[]): EventEmitter;
88
}
99

10-
interface GrpcFunctionObject extends GrpcFunction {
10+
export interface GrpcFunctionObject extends GrpcFunction {
1111
requestStream: boolean;
1212
responseStream: boolean;
1313
originalName: string;
@@ -21,7 +21,7 @@ interface CreateStubFunc extends CallableFunction {
2121
(createStub: unknown, options: StubOptions): PromiseLike<Stub>;
2222
}
2323

24-
interface Stub {
24+
export interface Stub {
2525
[key: string]: GrpcFunctionObject;
2626
}
2727

@@ -78,7 +78,7 @@ function wrapCreateStub(origCreate: CreateStubFunc): CreateStubFunc {
7878
}
7979

8080
/** Patches the function in grpc stub to enable tracing */
81-
function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: string): void {
81+
export function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: string): void {
8282
const funcObj = stub[methodName];
8383
if (typeof funcObj !== 'function') {
8484
return;

packages/google-cloud-serverless/test/__mocks__/dns.ts

-2
This file was deleted.

packages/google-cloud-serverless/test/gcpfunction/cloud_event.test.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
2+
import { describe, vi, beforeEach, test, expect } from 'vitest';
23

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

6-
const mockStartSpanManual = jest.fn((...spanArgs) => ({ ...spanArgs }));
7-
const mockFlush = jest.fn((...args) => Promise.resolve(args));
8-
const mockCaptureException = jest.fn();
7+
const mockStartSpanManual = vi.fn((...spanArgs) => ({ ...spanArgs }));
8+
const mockFlush = vi.fn((...args) => Promise.resolve(args));
9+
const mockCaptureException = vi.fn();
910

1011
const mockScope = {
11-
setContext: jest.fn(),
12+
setContext: vi.fn(),
1213
};
1314

1415
const mockSpan = {
15-
end: jest.fn(),
16+
end: vi.fn(),
1617
};
1718

18-
jest.mock('@sentry/node', () => {
19-
const original = jest.requireActual('@sentry/node');
19+
vi.mock('@sentry/node', async () => {
20+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
21+
const original = (await vi.importActual('@sentry/node')) as typeof import('@sentry/node');
2022
return {
2123
...original,
2224
startSpanManual: (...args: unknown[]) => {
@@ -38,7 +40,7 @@ jest.mock('@sentry/node', () => {
3840

3941
describe('wrapCloudEventFunction', () => {
4042
beforeEach(() => {
41-
jest.clearAllMocks();
43+
vi.clearAllMocks();
4244
});
4345

4446
function handleCloudEvent(fn: CloudEventFunctionWithCallback): Promise<any> {

packages/google-cloud-serverless/test/gcpfunction/events.test.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
2+
import { describe, vi, beforeEach, test, expect } from 'vitest';
23

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

7-
const mockStartSpanManual = jest.fn((...spanArgs) => ({ ...spanArgs }));
8-
const mockFlush = jest.fn((...args) => Promise.resolve(args));
9-
const mockCaptureException = jest.fn();
8+
const mockStartSpanManual = vi.fn((...spanArgs) => ({ ...spanArgs }));
9+
const mockFlush = vi.fn((...args) => Promise.resolve(args));
10+
const mockCaptureException = vi.fn();
1011

1112
const mockScope = {
12-
setContext: jest.fn(),
13+
setContext: vi.fn(),
1314
};
1415

1516
const mockSpan = {
16-
end: jest.fn(),
17+
end: vi.fn(),
1718
};
1819

19-
jest.mock('@sentry/node', () => {
20-
const original = jest.requireActual('@sentry/node');
20+
vi.mock('@sentry/node', async () => {
21+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
22+
const original = (await vi.importActual('@sentry/node')) as typeof import('@sentry/node');
2123
return {
2224
...original,
2325
startSpanManual: (...args: unknown[]) => {
@@ -39,7 +41,7 @@ jest.mock('@sentry/node', () => {
3941

4042
describe('wrapEventFunction', () => {
4143
beforeEach(() => {
42-
jest.clearAllMocks();
44+
vi.clearAllMocks();
4345
});
4446

4547
function handleEvent(fn: EventFunctionWithCallback): Promise<any> {
@@ -238,7 +240,7 @@ describe('wrapEventFunction', () => {
238240
const scopeFunction = mockCaptureException.mock.calls[0][1];
239241
const event: Event = { exception: { values: [{}] } };
240242
let evtProcessor: ((e: Event) => Event) | undefined = undefined;
241-
scopeFunction({ addEventProcessor: jest.fn().mockImplementation(proc => (evtProcessor = proc)) });
243+
scopeFunction({ addEventProcessor: vi.fn().mockImplementation(proc => (evtProcessor = proc)) });
242244

243245
expect(evtProcessor).toBeInstanceOf(Function);
244246
// @ts-expect-error just mocking around...

packages/google-cloud-serverless/test/gcpfunction/http.test.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
import type { Integration } from '@sentry/core';
2-
32
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
3+
import { describe, vi, beforeEach, test, expect, type MockInstance } from 'vitest';
44

55
import { wrapHttpFunction } from '../../src/gcpfunction/http';
6-
76
import type { HttpFunction, Request, Response } from '../../src/gcpfunction/general';
8-
97
import { init } from '../../src/sdk';
108

11-
const mockStartSpanManual = jest.fn((...spanArgs) => ({ ...spanArgs }));
12-
const mockFlush = jest.fn((...args) => Promise.resolve(args));
13-
const mockCaptureException = jest.fn();
14-
const mockInit = jest.fn();
9+
const mockStartSpanManual = vi.fn((...spanArgs) => ({ ...spanArgs }));
10+
const mockFlush = vi.fn((...args) => Promise.resolve(args));
11+
const mockCaptureException = vi.fn();
12+
const mockInit = vi.fn();
1513

1614
const mockScope = {
17-
setSDKProcessingMetadata: jest.fn(),
15+
setSDKProcessingMetadata: vi.fn(),
1816
};
1917

2018
const mockSpan = {
21-
end: jest.fn(),
19+
end: vi.fn(),
2220
};
2321

24-
jest.mock('@sentry/node', () => {
25-
const original = jest.requireActual('@sentry/node');
22+
vi.mock('@sentry/node', async () => {
23+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
24+
const original = (await vi.importActual('@sentry/node')) as typeof import('@sentry/node');
2625
return {
2726
...original,
2827
init: (options: unknown) => {
@@ -47,7 +46,7 @@ jest.mock('@sentry/node', () => {
4746

4847
describe('GCPFunction', () => {
4948
beforeEach(() => {
50-
jest.clearAllMocks();
49+
vi.clearAllMocks();
5150
});
5251

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

149-
const mockEnd = jest.fn();
148+
const mockEnd = vi.fn();
150149
const response = { end: mockEnd } as unknown as Response;
151150

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

172171
await handleHttp(wrappedHandler);
173172

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

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

0 commit comments

Comments
 (0)