Jest.js Prisma Client嘲笑笑话:TypeError:mock.mockReset不是函数

mf98qq94  于 2023-05-04  发布在  Jest
关注(0)|答案(1)|浏览(170)

尝试模拟PrismaClient时,出现以下错误。
“TypeError:mock.mockReset is not a function”,当它命中beforeEach/ mockReset函数时。
我的代码很简单。

import { PrismaClient } from '@prisma/client'
import {  mockDeep, DeepMockProxy, mockReset} from 'jest-mock-extended'

import { prisma } from './client'

jest.mock('./client', () => ({
  __esModule: true,
  default: mockDeep<PrismaClient>(),
}))

beforeEach(() => {
    mockReset(prismaMock)
  })

export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>

我在客户端文件中的代码也很简单:

import { PrismaClient } from '@prisma/client'

export const prismaClient = new PrismaClient()
export const prisma = prismaClient
vc9ivgsu

vc9ivgsu1#

解决方案

更新模拟以匹配指定的导出:

jest.mock('./client', () => ({
  __esModule: true,
  prisma: mockDeep<PrismaClient>()
}))

说明

模拟导出与实际文件导出不一致:

客户端文件

export const prisma = prismaClient; // named export: "prisma"

模拟客户端文件

jest.mock('./client', () => ({
  __esModule: true,
  default: mockDeep<PrismaClient>(), // default export which needs to match the named export ('prisma')
}))

因此,当jest尝试重置mock时,它会在一个名为“prisma”的命名导出上调用.mockReset,但它不存在(因此不是函数错误):而是存在于默认导出中。

相关问题