Skip to content

Commit

Permalink
Add tests for @prisma/client not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
chtushar committed Apr 25, 2024
1 parent b4427bb commit 35570e3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
rules: {
'func-call-spacing': 'off',
'space-before-function-paren': 'off',
'@typescript-eslint/no-var-requires': 'off'
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/ban-ts-comment': 'off'
}
};
13 changes: 8 additions & 5 deletions src/OpenAPM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import { getHostIpAddress, getPackageJson, getSanitizedPath } from './utils';
import { instrumentExpress } from './clients/express';
import { instrumentMySQL } from './clients/mysql2';
import { instrumentNestFactory } from './clients/nestjs';
import { LevitateConfig, LevitateEvents } from './levitate/events';
import { instrumentNextjs } from './clients/nextjs';

import { LevitateConfig, LevitateEvents } from './levitate/events';

export type ExtractFromParams = {
from: 'params';
key: string;
Expand Down Expand Up @@ -85,7 +86,7 @@ const moduleNames = {
const packageJson = getPackageJson();

export class OpenAPM extends LevitateEvents {
private simpleCache: Record<string, any> = {};
readonly simpleCache: Record<string, any> = {};
private path: string;
private metricsServerPort: number;
private enabled: boolean;
Expand Down Expand Up @@ -189,8 +190,6 @@ export class OpenAPM extends LevitateEvents {
console.log('Shutting down metrics server gracefully.');
}
this.metricsServer?.close((err) => {
promClient.register.clear();

if (err) {
reject(err);
return;
Expand All @@ -199,6 +198,9 @@ export class OpenAPM extends LevitateEvents {
resolve(undefined);
console.log('Metrics server shut down gracefully.');
});

promClient.register.clear();
resolve(undefined);
});
};

Expand Down Expand Up @@ -348,6 +350,8 @@ export class OpenAPM extends LevitateEvents {
const prisma = new PrismaClient();
const prismaMetrics = prisma ? await prisma.$metrics.prometheus() : '';
metrics += prisma ? prismaMetrics : '';

await prisma.$disconnect();
} catch (error) {
this.simpleCache['prisma:installed'] = false;
}
Expand Down Expand Up @@ -392,7 +396,6 @@ export class OpenAPM extends LevitateEvents {
}
} catch (error) {
if (Object.keys(moduleNames).includes(moduleName)) {
console.log(error);
throw new Error(
`OpenAPM couldn't import the ${moduleNames[moduleName]} package, please install it.`
);
Expand Down
6 changes: 3 additions & 3 deletions tests/mysql2.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, beforeAll, expect, test, vi, afterAll } from 'vitest';
import mysql2, { Connection, Pool, PoolCluster, PoolNamespace } from 'mysql2';
import { describe, beforeAll, expect, test, afterAll } from 'vitest';
import mysql2, { Connection, Pool, PoolCluster } from 'mysql2';
import { instrumentMySQL, symbols } from '../src/clients/mysql2';
import prom, { Histogram } from 'prom-client';

const connectionUri = `mysql://root@localhost:3306/test_db`;
const connectionUri = 'mysql://root@localhost:3306/test_db';

const sendTestRequest = async (conn: Connection | Pool, query: string) => {
return new Promise((resolve) => {
Expand Down
44 changes: 44 additions & 0 deletions tests/prisma.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import express, { Express } from 'express';
import { test, expect, describe, beforeAll, afterAll, vi } from 'vitest';

import OpenAPM from '../src/OpenAPM';
import { addRoutes, makeRequest } from './utils';

describe('Prisma', () => {
let openapm: OpenAPM;
let app: Express;

beforeAll(async () => {
openapm = new OpenAPM({
enableMetricsServer: false
});
openapm.instrument('express');

app = express();

app.get('/metrics', async (req, res) => {
res.status(200).send(await openapm.getMetrics());
});

addRoutes(app);
app.listen(3002);
});

afterAll(async () => {
await openapm.shutdown();
});

test('prisma:installed - false', async () => {
vi.doMock('@prisma/client', async () => {
throw new Error('Cannot find module @prisma/client');
});
await makeRequest(app, '/api/10');
await makeRequest(app, '/metrics');

expect(openapm.simpleCache['prisma:installed']).toBe(false);
});

test('simpleCache', async () => {
expect(openapm.simpleCache['prisma:installed']).toBe(false);
});
});

0 comments on commit 35570e3

Please sign in to comment.