如何在单元测试中模拟 Prisma Promise?

Rom*_*kyi 5 prisma

假设我对数据库进行了事务更新

\n
...\n\nconst sendMoney = prisma.account.update({ where: { id: 1 }, data: { ... } })\nconst receiveMoney = prisma.account.update({ where: { id: 2 }, data: { ... } })\n\nconst [accountOfSender, accountOfReceiver] = await prisma.$transaction([\n  sendMoney,\n  receiveMoney\n])\n\n...\n
Run Code Online (Sandbox Code Playgroud)\n

我有一个单元测试来验证这笔交易

\n
describe(\'The money transfer use case\', () => {\n  let mockedContext: MockContext\n  let moneyTransferUseCase: MoneyTransferUseCase\n\n  beforeEach(() => {\n    mockedContext = createMockedContext()\n    moneyTransferUseCase = new MoneyTransferUseCase(mockedContext.prisma)\n  })\n\n  it(\'should transfer money between two accounts\', () => {\n    ...\n\n    const updatedAccount = { ... }\n\n    mockedContext.prisma.account.update.mockReturnedValue(Promise.resolve(updatedAccount))\n                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - throws an error\n\n    ...\n  })\n
Run Code Online (Sandbox Code Playgroud)\n

错误信息

\n
TS2345: Argument of type \'Promise<Account>\' is not assignable to parameter of type \'Prisma__AccountClinet<Account>\'.\n      Type \'Promise<Account>\' is missing the following properties from type \'Prisma__AccountClient<Account>\': _dmmf, _fetcher, _queryType, _rootField, and 11 more.\n\n    63     mockedContext.prisma.account.update.mockReturnValue(Promise.resolve(updatedAccount))\n
Run Code Online (Sandbox Code Playgroud)\n

这个错误消息中最重要的是这个

\n
Type \'Promise<Account>\' is missing the following properties from type \'Prisma__AccountClient<Account>\': _dmmf, _fetcher, _queryType, _rootField, and 11 more.\n
Run Code Online (Sandbox Code Playgroud)\n

我可以理解Promise.resolve(updatedAccount)不足以嘲笑棱镜承诺。但如何嘲笑它呢?文档对此没有任何说明。有任何想法吗?

\n

更新

\n

我想了一会儿,决定我可以嘲笑prisma.$transaction([ ... ])调用本身

\n
TS2345: Argument of type \'Promise<Account>\' is not assignable to parameter of type \'Prisma__AccountClinet<Account>\'.\n      Type \'Promise<Account>\' is missing the following properties from type \'Prisma__AccountClient<Account>\': _dmmf, _fetcher, _queryType, _rootField, and 11 more.\n\n    63     mockedContext.prisma.account.update.mockReturnValue(Promise.resolve(updatedAccount))\n
Run Code Online (Sandbox Code Playgroud)\n

它准确返回我需要的内容,但中间调用

\n
Type \'Promise<Account>\' is missing the following properties from type \'Prisma__AccountClient<Account>\': _dmmf, _fetcher, _queryType, _rootField, and 11 more.\n
Run Code Online (Sandbox Code Playgroud)\n

undefined测试期间返回。除了将这些结果传递给prisma.$transaction

\n

所以看起来测试按预期工作,但不确定这是否是一个好方法。

\n