I am trying to unit test my database based on the Prisma unit testing documentation. When I run my tests though, calls are actually going out to the database. I was under the impression mocking the Prisma client would prevent that.
I wrote code to initialize the Prisma client.
import { PrismaClient } from "@prisma/client"
let prisma
if(process.env.NODE_ENV === 'production') {
prisma = new PrismaClient()
} else {
if(!global.prisma) {
global.prisma = new PrismaClient()
}
prisma = global.prisma
}
export default prisma
I wrote code to create an entry in the database.
export async function createTest(id) {
const result = await await prisma.test.create({
data: {
id: id,
},
})
return result
}
And I wrote code to test the create function.
import { createTest } from '../lib/crud'
import { prismaMock } from '../lib/mock/singleton'
it('confirms an entry can be created in the database', async () => {
const id = 1
prismaMock.test.create.mockResolvedValue(id)
await expect(createTest(id)).resolves.toEqual({
id: 1,
})
})
Am I missing something? Why is the create function actually creating entries in the database when the tests run?