Prisma 2 不适用于 NestJS 规范测试

Posted

技术标签:

【中文标题】Prisma 2 不适用于 NestJS 规范测试【英文标题】:Prisma 2 not working well with NestJS spec testing 【发布时间】:2021-12-21 16:01:35 【问题描述】:

我正在使用 NestJS,并使用 Prisma 2 进行设置。我现在的问题是测试。我失败的测试都与 PrismaService 中不存在的实体对象有关。例如,每个失败的测试看起来都像这样,但可能是不同的实体类型:

src/user/user.service.ts:25:24 - error TS2339: Property 'user' does not exist on type 'PrismaService'.

    25     return this.prisma.user.findUnique( where: userWhereUniqueInput );

正在运行的测试是 Nest cli 为资源生成的默认测试。我的 PrismaService 只是一个扩展 PrismaClient 并覆盖关闭挂钩方法和 init/destroy 生命周期方法的类,正如 nestJS 在他们的 Prisma2 示例中提供的那样。这是模块导入/导出问题还是只是 prisma/nestjs/jest 之间的兼容性问题?我的模块都导入所需的模块并导出它们的服务/提供程序,所以我不确定这里发生了什么,除非需要进行一些预测试设置。

【问题讨论】:

【参考方案1】:

事实证明,我并没有在我的测试套件中导入 PrismaModule。此外,导致这些失败的次要问题是使用绝对路径 (/src/prisma/prisma.module) 而不是相对路径 (../prisma/prisma.module) 导入 PrismaService

【讨论】:

primsa 模块的导入对这里有什么帮助?我很讨厌Nest can't resolve dependencies of the MeasurementsService (?). Please make sure that the argument PrismaService at index [0] is available in the RootTestModule context.

以上是关于Prisma 2 不适用于 NestJS 规范测试的主要内容,如果未能解决你的问题,请参考以下文章

使用 Prisma 2 和 NestJS 进行日志记录 - 依赖注入问题?

findFirst 不适用于 Prisma 上的 3 个过滤器

prisma.exists 方法不适用于 mongodb Atlas

NestJS-Prisma,如何编写与 prisma 一对多类型匹配的 DTO

Nestjs:验证功能不适用于 jwt

NestJS 中与 GraphQL 的 Prisma 自关系