我正在尝试使用Prisma博士来设置我的测试环境,这有点像一场火车灾难。所以,我在向后工作,试图找出为什么它不起作用。下面显示了singleton.ts文件的设置,当我尝试启动测试时,它失败了,出现了以下错误: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', () => {
//
});创建此错误:

发布于 2022-02-23 16:23:24
我继续研究这个bug,发现我的解决方案名为导出,而不是默认导出new PrismaClient实例。在这个回购中,我有一个基于文档的工作示例:https://github.com/KevinKra/prisma-test-debug-example。
此外,我在这里开了一篇关于Prisma回购的文章还详细介绍了我在途中找到的解决方案。
简而言之,在我的特定情况下,我需要执行如下默认导出:
import { PrismaClient } from '@prisma/client';
const $prisma = new PrismaClient();
export default $prisma;还为我的globals文件提供了一个jest.config.js属性,这样它就可以实际插入到我的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
},
},
};如果我的答案不能解决您的问题,I强烈建议创建一个新的回购程序,并通过步骤确认确实使用普通的在您的计算机上传递。
https://stackoverflow.com/questions/71190104
复制相似问题