Mock What Prisma Returns, Not What Your API Returns
I'm building RunHop in public — a social + event platform for running races, built on NestJS. Today I finished the organization and membership e2e test suites. Along the way, I spent an embarrassin...
Source: dev.to
I'm building RunHop in public — a social + event platform for running races, built on NestJS. Today I finished the organization and membership e2e test suites. Along the way, I spent an embarrassing amount of time on a TypeError that came down to one wrong mock. The Crash // organization.service.ts async list(cursor?: string, take: number = 20) { const args: Prisma.OrganizationFindManyArgs = { take, where: { deletedAt: null }, orderBy: { createdAt: 'desc' } }; if (cursor) { args.skip = 1; args.cursor = { id: cursor } } const result = await this.prisma.organization.findMany(args); const nextCursor = result.at(-1)?.id; return { data: result, meta: { cursor: nextCursor } }; } The unit test: it('should return orgs without cursor', async () => { mockPrisma.organization.findMany.mockResolvedValue( { data: [mockOrg], nextCursor: mockOrg.id } // <-- the problem ); const result = await service.list(); // TypeError: result.at is not a function }); result.at(-1) crashed because the mock ret