From 32e4b3edf89860c7b4df3562a3bd45e03c574390 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Thu, 22 Jun 2023 15:41:09 +0200 Subject: [PATCH] Fix tests --- .../src/node/integrations/prisma.ts | 9 +++--- .../test/integrations/node/prisma.test.ts | 29 ++++++++++++------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/tracing-internal/src/node/integrations/prisma.ts b/packages/tracing-internal/src/node/integrations/prisma.ts index 0601d8e22b8d..ab6fd1f1ae5b 100644 --- a/packages/tracing-internal/src/node/integrations/prisma.ts +++ b/packages/tracing-internal/src/node/integrations/prisma.ts @@ -67,12 +67,11 @@ export class Prisma implements Integration { if (isValidPrismaClient(options.client) && !options.client._sentryInstrumented) { addNonEnumerableProperty(options.client as any, '_sentryInstrumented', true); - if (shouldDisableAutoInstrumentation(getCurrentHub)) { - __DEBUG_BUILD__ && logger.log('Prisma Integration is skipped because of instrumenter configuration.'); - return; - } - options.client.$use((params, next: (params: PrismaMiddlewareParams) => Promise) => { + if (shouldDisableAutoInstrumentation(getCurrentHub)) { + return next(params); + } + const action = params.action; const model = params.model; return trace( diff --git a/packages/tracing/test/integrations/node/prisma.test.ts b/packages/tracing/test/integrations/node/prisma.test.ts index 953947f7df5d..b3bd30c49f75 100644 --- a/packages/tracing/test/integrations/node/prisma.test.ts +++ b/packages/tracing/test/integrations/node/prisma.test.ts @@ -1,7 +1,10 @@ /* eslint-disable deprecation/deprecation */ +import * as sentryCore from '@sentry/core'; +import { Hub } from '@sentry/core'; import { logger } from '@sentry/utils'; import { Integrations } from '../../../src'; +import { getTestClient } from '../../testutils'; const mockTrace = jest.fn(); @@ -35,18 +38,15 @@ class PrismaClient { } describe('setupOnce', function () { - const Client: PrismaClient = new PrismaClient(); - - beforeAll(() => { - new Integrations.Prisma({ client: Client }); - }); - beforeEach(() => { mockTrace.mockClear(); + mockTrace.mockReset(); }); it('should add middleware with $use method correctly', done => { - void Client.user.create()?.then(() => { + const prismaClient = new PrismaClient(); + new Integrations.Prisma({ client: prismaClient }); + void prismaClient.user.create()?.then(() => { expect(mockTrace).toHaveBeenCalledTimes(1); expect(mockTrace).toHaveBeenLastCalledWith( { name: 'user create', op: 'db.sql.prisma', data: { 'db.system': 'prisma' } }, @@ -56,11 +56,18 @@ describe('setupOnce', function () { }); }); - it("doesn't attach when using otel instrumenter", () => { - const loggerLogSpy = jest.spyOn(logger, 'log'); + it("doesn't trace when using otel instrumenter", done => { + const prismaClient = new PrismaClient(); + new Integrations.Prisma({ client: prismaClient }); - new Integrations.Prisma({ client: Client }); + const client = getTestClient({ instrumenter: 'otel' }); + const hub = new Hub(client); - expect(loggerLogSpy).toBeCalledWith('Prisma Integration is skipped because of instrumenter configuration.'); + jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub); + + void prismaClient.user.create()?.then(() => { + expect(mockTrace).not.toHaveBeenCalled(); + done(); + }); }); });