处理Nestjs测试中的多个实例Prisma警告
我正在编写Nestjs API的测试。当我运行它时,它会产生此错误:
warn(prisma-client) There are already 10 instances of Prisma Client actively running.
这是在我的测试中实例化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();
});
以及我的Prisma服务:
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();
}
}
我将Prisma服务注入用户服务中,到目前为止,我已经写了11次测试。我需要在每次运行之前清除客户表,如果我不清除它,我不会遇到此错误。您可以帮助我处理此错误或提供解决方案而不是清理表吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论