Hi guys! Can anyone help me with unit testing? I f...
# orm-help
m
Hi guys! Can anyone help me with unit testing? I followed the steps described in the docs to create the
prismaMock
but it's not working
Here's the code:
tests/.../prisma/helpers/singleton.ts
Copy code
import { PrismaClient } from '@prisma/client';
import { mockDeep, mockReset, DeepMockProxy } from 'jest-mock-extended';

import prisma from '@infra/data/postgres/prisma/helpers/prismaClient';

jest.mock('@infra/data/postgres/prisma/helpers/prismaClient', () => ({
  __esModule: true,
  default: mockDeep<PrismaClient>(),
}));

beforeEach(() => {
  mockReset(prismaMock);
});

export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>;
AccountRepository.spec
Copy code
beforeAll(() => {
      email = '<mailto:valid@mail.com|valid@mail.com>';

      prismaMock.account.findUnique.mockResolvedValue({
        id: 'validId',
        email: '<mailto:valid@mail.com|valid@mail.com>',
        password: 'validPassword',
        active: true,
        role: 'STUDENT',
        createdAt: new Date(),
        updatedAt: new Date(),
      });
    });

    it('should return an account if email exists', async () => {
      const account = await sut.findByEmail({ email });

      expect(account).toBeTruthy();
    });
The repo just calls the prisma.account.findUnique, but the value returned by this method is not the mocked
I already wasted a couple of hours trying to fix this
I had success by implementing something like this:
Copy code
prismaClient.account.findUnique = jest.fn().mockResolvedValue({
        id: 'validId',
        email: '<mailto:valid@mail.com|valid@mail.com>',
        password: 'validPassword',
        active: true,
        role: 'STUDENT',
        createdAt: new Date(),
        updatedAt: new Date(),
      });
but not sure if this is the correct way
and in this case I don't use the "prismaMock" as docs suggest at all
j
Could you open a discussion here maybe? https://github.com/prisma/prisma/discussions