prisma 2 testing -- TypeError: Cannot read property '_isMockObject' of undefined

1.1k Views Asked by At

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:

enter image description here

1

There are 1 best solutions below

0
On

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:

import { PrismaClient } from '@prisma/client';

const $prisma = new PrismaClient();
export default $prisma;

and also provide a globals attribute for my jest.config.js file so it can actually plug into my tsconfig.

module.exports = {
    clearMocks: true,
    preset: 'ts-jest',
    testEnvironment: 'node',
    setupFilesAfterEnv: ['<rootDir>/src/tests/config.ts'],
    verbose: true,
    globals: {
        'ts-jest': {
            tsconfig: './.tsconfig.json',
            // set global config for ts-jest
        },
    },
};

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.