jest-prisma
jest-prisma copied to clipboard
Can I use a singleton and an extended client?
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!
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).
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);.