I'm writing tests for a Nestjs API. when I run it, it gives this error:
warn(prisma-client) There are already 10 instances of Prisma Client actively running.
this is in my test where I instantiate Prisma:
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UserController],
providers: [UserService, PrismaService, RedisService],
imports: [
ConfigModule.forRoot({
envFilePath: `${process.cwd()}/.env.${process.env.NODE_ENV}`,
load: [configuration],
cache: true,
}),
],
}).compile();
controller = module.get<UserController>(UserController);
service = module.get<UserService>(UserService);
redis = module.get<RedisService>(RedisService);
prisma = module.get<PrismaService>(PrismaService);
await redis.onModuleInit();
await prisma.customer.deleteMany({});
});
afterEach(async () => {
await redis.onModuleDestroy();
await prisma.onModuleDestroy();
});
and my prisma service:
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy
{
constructor() {
super();
}
async onModuleInit() {
try {
await this.$connect();
} catch (e) {
console.log(e);
}
}
async onModuleDestroy() {
await this.$disconnect();
}
}
I inject and use prisma service into user service and I've written 11 tests by now. I need to clear customer table before each run and if I don't clear it, I don't get this error.can you help me with handling this error or offering a solution instead of clearing table?