Skip to content

Commit ebceb64

Browse files
onurtemizkanAbhiPrasad
authored andcommitted
test(tracing): Add Prisma ORM integration tests. (#4962)
Add integration tests for Prisma ORM integration based on https://www.prisma.io/docs/guides/testing/integration-testing. Also update integration perform validation on Prisma Client.
1 parent f8c64a3 commit ebceb64

File tree

13 files changed

+203
-3
lines changed

13 files changed

+203
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

packages/node-integration-tests/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
},
88
"private": true,
99
"scripts": {
10+
"clean": "rimraf -g **/node_modules",
11+
"prisma:init": "(cd suites/tracing/prisma-orm && ts-node ./setup.ts)",
1012
"lint": "run-s lint:prettier lint:eslint",
1113
"lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish",
1214
"lint:prettier": "prettier --check \"{suites,utils}/**/*.ts\"",
1315
"type-check": "tsc",
16+
"pretest": "run-s --silent prisma:init",
1417
"test": "jest --runInBand --forceExit",
1518
"test:watch": "yarn test --watch"
1619
},
1720
"dependencies": {
21+
"@prisma/client": "^3.12.0",
1822
"@types/mongodb": "^3.6.20",
1923
"@types/mysql": "^2.15.21",
2024
"@types/pg": "^8.6.5",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.9'
2+
3+
services:
4+
db:
5+
image: postgres:13
6+
restart: always
7+
container_name: integration-tests-prisma
8+
ports:
9+
- '5433:5432'
10+
environment:
11+
POSTGRES_USER: prisma
12+
POSTGRES_PASSWORD: prisma
13+
POSTGRES_DB: tests
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "sentry-prisma-test",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"engines": {
7+
"node": ">=12"
8+
},
9+
"scripts": {
10+
"db-up": "docker-compose up -d",
11+
"generate": "prisma generate",
12+
"migrate": "prisma migrate dev -n sentry-test",
13+
"setup": "run-s --silent db-up generate migrate"
14+
},
15+
"keywords": [],
16+
"author": "",
17+
"license": "ISC",
18+
"dependencies": {
19+
"@prisma/client": "3.12.0",
20+
"prisma": "^3.12.0"
21+
}
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "postgresql"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- CreateTable
2+
CREATE TABLE "User" (
3+
"id" SERIAL NOT NULL,
4+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
5+
"email" TEXT NOT NULL,
6+
"name" TEXT,
7+
8+
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
9+
);
10+
11+
-- CreateIndex
12+
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
datasource db {
2+
url = "postgresql://prisma:prisma@localhost:5433/tests"
3+
provider = "postgresql"
4+
}
5+
6+
generator client {
7+
provider = "prisma-client-js"
8+
}
9+
10+
model User {
11+
id Int @id @default(autoincrement())
12+
createdAt DateTime @default(now())
13+
email String @unique
14+
name String?
15+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2+
import { PrismaClient } from '@prisma/client';
3+
import * as Sentry from '@sentry/node';
4+
import * as Tracing from '@sentry/tracing';
5+
import { randomBytes } from 'crypto';
6+
7+
const client = new PrismaClient();
8+
9+
Sentry.init({
10+
dsn: 'https://[email protected]/1337',
11+
release: '1.0',
12+
tracesSampleRate: 1.0,
13+
integrations: [new Tracing.Integrations.Prisma({ client })],
14+
});
15+
16+
async function run(): Promise<void> {
17+
const transaction = Sentry.startTransaction({
18+
name: 'Test Transaction',
19+
op: 'transaction',
20+
});
21+
22+
Sentry.configureScope(scope => {
23+
scope.setSpan(transaction);
24+
});
25+
26+
try {
27+
await client.user.create({
28+
data: {
29+
name: 'Tilda',
30+
email: `tilda_${randomBytes(4).toString('hex')}@sentry.io`,
31+
},
32+
});
33+
34+
await client.user.findMany();
35+
36+
await client.user.deleteMany({
37+
where: {
38+
email: {
39+
contains: 'sentry.io',
40+
},
41+
},
42+
});
43+
} finally {
44+
if (transaction) transaction.finish();
45+
}
46+
}
47+
48+
void run();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { parseSemver } from '@sentry/utils';
2+
import { execSync } from 'child_process';
3+
4+
const NODE_VERSION = parseSemver(process.versions.node);
5+
6+
if (NODE_VERSION.major && NODE_VERSION.major < 12) {
7+
// eslint-disable-next-line no-console
8+
console.warn(`Skipping Prisma tests on Node: ${NODE_VERSION.major}`);
9+
process.exit(0);
10+
}
11+
12+
try {
13+
execSync('yarn && yarn setup');
14+
} catch (_) {
15+
process.exit(1);
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { assertSentryTransaction, conditionalTest, getEnvelopeRequest, runServer } from '../../../utils';
2+
3+
conditionalTest({ min: 12 })('Prisma ORM Integration', () => {
4+
test('should instrument Prisma client for tracing.', async () => {
5+
const url = await runServer(__dirname);
6+
const envelope = await getEnvelopeRequest(url);
7+
8+
assertSentryTransaction(envelope[2], {
9+
transaction: 'Test Transaction',
10+
spans: [
11+
{ description: 'User create', op: 'db.prisma' },
12+
{ description: 'User findMany', op: 'db.prisma' },
13+
{ description: 'User deleteMany', op: 'db.prisma' },
14+
],
15+
});
16+
});
17+
});

0 commit comments

Comments
 (0)