diff --git a/src/OpenAPM.ts b/src/OpenAPM.ts index 98ab9d8..b12e2d2 100644 --- a/src/OpenAPM.ts +++ b/src/OpenAPM.ts @@ -86,7 +86,7 @@ const moduleNames = { const packageJson = getPackageJson(); export class OpenAPM extends LevitateEvents { - readonly simpleCache: Record = {}; + public simpleCache: Record = {}; private path: string; private metricsServerPort: number; private enabled: boolean; diff --git a/tests/prisma/prisma.test.ts b/tests/prisma/prisma.test.ts index 4a4ba6a..35f260a 100644 --- a/tests/prisma/prisma.test.ts +++ b/tests/prisma/prisma.test.ts @@ -5,6 +5,21 @@ import OpenAPM from '../../src/OpenAPM'; import { addRoutes, makeRequest } from '../utils'; import { Server } from 'http'; +class OpenAPMExtended extends OpenAPM { + public simpleCache: Record; + + constructor() { + super({ + enableMetricsServer: false + }); + this.simpleCache = {}; + } + + clearSimpleCache() { + this.simpleCache = {}; + } +} + async function mock(mockedUri, stub) { const { Module } = await import('module'); @@ -33,10 +48,18 @@ describe('Prisma', () => { let app: Express; let server: Server; - beforeAll(async () => { - openapm = new OpenAPM({ - enableMetricsServer: false + vi.hoisted(async () => { + await mock('@prisma/client', { + PrismaClient: class PrismaClient { + constructor() { + throw new Error('Cannot find module "@prisma/client"'); + } + } }); + }); + + beforeAll(async () => { + openapm = new OpenAPMExtended(); openapm.instrument('express'); app = express(); @@ -67,11 +90,8 @@ describe('Prisma', () => { test('metrics', async () => { await unmock(); - await openapm.shutdown(); - openapm = new OpenAPM({ - enableMetricsServer: false - }); - const res = await makeRequest(app, '/metrics'); - expect(res.text).toContain('prisma_pool_connections_busy'); + (openapm as OpenAPMExtended).clearSimpleCache(); + await makeRequest(app, '/metrics'); + expect(openapm.simpleCache['prisma:installed']).toBe(true); }); });