Mock Prisma client #2152
Answered
by
Romakita
thermsdorff
asked this question in
Q&A
Mock Prisma client
#2152
-
Hello ! I need to mock prisma client to avoid database connection during tests. But since the instantiation of the Prisma client is handled by @tsed/prisma what is the best way to mock Prisma client while using tsed ? |
Beta Was this translation helpful? Give feedback.
Answered by
Romakita
Nov 10, 2022
Replies: 1 comment 5 replies
-
Hello @thermsdorff, You can try that: import { PrismaClient } from '@prisma/client'
import { mockDeep, mockReset, DeepMockProxy } from 'jest-mock-extended'
export function mockPrismaService() {
registerProvider({
provide: PrismaService,
useClass: null,
useFactory: () => {
return mockDeep<PrismaClient>()
},
hooks: {
$onDestroy(instance: any) {
mockReset(instance);
}
}
})
}
export function restorePrismaService() {
registerProvider({provide: PrismaService, useClass: PrismaService})
} |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
thermsdorff
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @thermsdorff,
You can try that: