jest-prisma icon indicating copy to clipboard operation
jest-prisma copied to clipboard

Can I use a singleton and an extended client?

Open dmaksimov opened this issue 11 months ago • 2 comments

Hi,

I'm using a singleton for my prisma client. I also have some extensions on it that I need during my tests.

In my jest.setup.ts file i have

jest.mock('./src/services/prisma', () => {
  return {
    prisma: jestPrisma.client,
  };
});

I tried call initializeClient above that.

import { prisma } from '@/services/prisma';

jestPrisma.initializeClient(prisma);

jest.mock('./src/services/prisma', () => {
  return {
    prisma: jestPrisma.client,
  };
});

but I get this error: TypeError: this.originalClient.$connect is not a function

I also tried calling initializeClient inside the mock

jest.mock('./src/services/prisma', () => {
  jestPrisma.initializeClient(prisma);
  
  return {
    prisma: jestPrisma.client,
  };
});

but I'm getting this error: ReferenceError: Cannot access '_prisma' before initialization.

Any help would be greatly appreciated!

dmaksimov avatar Dec 12 '24 02:12 dmaksimov

Same issue here, calling only jestPrisma.initializeClient(prisma); results in PrismaClient is unable to run in this browser environment, or has been bundled for the browser (running in Node.js).

james10424 avatar Mar 21 '25 19:03 james10424

I have figured out: The reason for TypeError: this.originalClient.$connect is not a function is that we are exporting prisma as default, so the mock statement should be: jest.mock('@/your/prisma', () => jestPrisma.client);.

james10424 avatar Mar 24 '25 19:03 james10424