处理Nestjs测试中的多个实例Prisma警告

发布于 2025-02-11 21:25:24 字数 1595 浏览 0 评论 0原文

我正在编写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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文