-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CustomPrismaService unit testing #83
Comments
Hi, I just started learning testing and using jest and I've encountered a similar problem and I think I've gotten a somewhat good grasp on the problem at hand. So basically, when testing an NestJS that depends on another provider what you need to do is tell NestJS how to construct it (mock it). There are multiple approaches to achieving that but here's what I did:
Then in your describe block of the unit you're testing create a mock value. Here I did it by just constructing a dummy Prisma.Customer[] object and then told the prismaMock.customer.findMany to mock the resolved value to be that instance.
Again, I've just started learning testing and using jest, but I hope this helps! |
If however you don't have an extended client or don't care about type-safety I encourage you to check out this blog: (https://wanago.io/2023/04/03/api-nestjs-unit-tests-prisma/) |
Hi! Thank you for
nestjs-prisma
, has been a great addition to our stack so far!We're using the
CustomPrismaService
because we need to extend the client. Sadly the unit test setup does not work, i'm struggling with mocking the client. Right now it errors withTypeError: Cannot read properties of undefined (reading 'user')
as the client is always undefined.The userService looks like this:
The userService.spec.test file like this:
Appreciative of any help, thank you so much!!
The text was updated successfully, but these errors were encountered: