I'm trying to set up my testing environment using the Prisma docs and it's been a bit of a trainwreck. So, I'm working backward trying to figure out why it's not working. I have the setup of the singleton.ts
file shown below, and when I try initiate my tests it fails with the following error: TypeError: Cannot read property '_isMockObject' of undefined
singleton.ts
import { PrismaClient } from '@prisma/client';
import { mockDeep, mockReset, DeepMockProxy } from 'jest-mock-extended';
// import { $prisma } from './client';
export const prisma = new PrismaClient();
jest.mock('./client', () => ({
__esModule: true,
default: mockDeep<PrismaClient>(),
}));
beforeEach(() => {
mockReset(prismaMock);
});
export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>;
someTest.test.ts
it('should pass', () => {
//
});
Creates this error:
I've continued working on this bug and found out that my solution was named exporting, instead of default exporting the instance of the
new PrismaClient
. I have a working example here in this repo that is based on the documentation: https://github.com/KevinKra/prisma-test-debug-example.additionally, I opened an issue here on the Prisma repo that details solutions I found along the way.
In short, in my particular case I needed to perform a default export like so:
and also provide a
globals
attribute for myjest.config.js
file so it can actually plug into my tsconfig.In the event my answer doesn't solve your issue, I highly recommend creating a new repo and working through the steps to confirm that tests do indeed pass on your computer using the vanilla Prisma setup.