我如何用es6模拟Prisma客户端(没有typescript) ?



Prisma文档中有模拟客户端和使用jest和typescript进行单元测试的示例。有没有办法在不使用TypeScript的情况下嘲笑客户端?

如果你能举一个简单的例子,我将不胜感激。

小事情要补充:我在我的项目中使用依赖注入在所有使用prisma的函数。

我做了一个Nodejs演示,它相当于官方指南-单元测试(TypeScript版本)。

见:https://github.com/OctobugDemo/nodejs-prisma-unit-test

这可能有帮助。

我现在的解决方案:我手动模拟数据库对象并将其传递给使用它的函数:我不确定这有多有用,但它有助于避免调用真正的数据库。

const mocked_db = {
user: {
findFirst: jest.fn(() => Promise.resolve(
{
id: 2,
first_name: "Basel",
last_name: "Akasha"
}
))
}
}
it("It should work blah blah bla", async () => {
let user_details= {
id: 2,
first_name: "Basel",
last_name: "Akasha"
}

let signup = await getUser(
mocked_db // takes thew DB object is a parameter (normally you would pass your Prisma client instance)
)
await expect(.... // whatever you'r expect is
})'

最新更新