제이의 블로그

Mocking Prisma Client for Unit Tests in NestJS

Created: 2024-04-02

Created: 2024-04-02 03:01

Application unit tests should ideally be conducted in isolation. Therefore, it's recommended to eliminate external dependencies like network I/O, databases, etc., that could potentially affect the test results.

In this toy project, I'm using Prisma ORM, and I'm writing this post to document a simple way to mock Prisma Client for unit testing purposes.


Installing the jest-mock-extended Package

This command installs the package recommended in the official Prisma documentation.


Mocking Prisma Client


Unit Test Example Code

After mocking, type support is also provided, making unit test writing incredibly convenient.

Comments0