Not sure if this is the right place, but I couldn’t think of where else to post this.
So I started using Jest and jest-mock-extended to do unit testing on some more complex functions in my code, and I’m, still a bit of a jest noob.
However I ran into an issue where I need to get nested data or something from prisma.
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.findManuy.mockResolvedValue([])
mockPrisma.person.findUniqueOrThrow.mockResolvedValue({
id: 1
name: 'John Smith',
posts: mockPrisma.post.findMany
});