Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Jun 22, 2023
1 parent 42c507b commit 32e4b3e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
9 changes: 4 additions & 5 deletions packages/tracing-internal/src/node/integrations/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>) => {
if (shouldDisableAutoInstrumentation(getCurrentHub)) {
return next(params);
}

const action = params.action;
const model = params.model;
return trace(
Expand Down
29 changes: 18 additions & 11 deletions packages/tracing/test/integrations/node/prisma.test.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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' } },
Expand All @@ -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();
});
});
});

0 comments on commit 32e4b3e

Please sign in to comment.