Jest integration nested values #24102
-
QuestionI'm using Jest and jest-mock-extended to do unit testing on some more complex functions in my code. However I ran into an issue where I need to get nested data or something from prisma. and I can't see anything on Prisma's official website about this and couldn't see anything, I also had a read through this article For example: primsa.person.findFirstOrThrow({where: {id: 1}}).posts()` When I just run this code: primsa.person.findFirstOrThrow({where: {id: 1}}) Jest works great and gets the mocked data, but when I add the .posts() I get an error saying TypeError: person.posts is not a function. I do my mocked person data like so: mockPrisma.person.findUniqueOrThrow.mockResolvedValue({
id: 1
name: 'John Smith'
}); I understand why this error occurs, but I can't work out the syntax to get the mocked posts in there as everything I try throws a TS error. Things I tried (assume data between the []): mockPrisma.person.findUniqueOrThrow.mockResolvedValue({
id: 1
name: 'John Smith',
posts: mockPrisma.post.findMany.mockResolvedValue([]);
});
////////////////////////////////////////////////////
mockPrisma.post.findMany.mockResolvedValue([])
mockPrisma.person.findUniqueOrThrow.mockResolvedValue({
id: 1
name: 'John Smith',
posts: mockPrisma.post.findMany
}); How to reproduce (optional)Expected behavior (optional)No response Information about Prisma Schema, Client Queries and Environment (optional)No response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @alexnsorensen 👋 Thank you for raising this question. Can you use prismaMock.person.findUniqueOrThrow.mockReturnValue({
posts: jest.fn().mockResolvedValue(posts),
}); |
Beta Was this translation helpful? Give feedback.
Hi, what I ended up having to do was this:
If I didn't add these then I would get a tonne of TS errors, so I Imagine it's to do with my TS settings probably being rather strict...