使用Node.js和Node postgresl更新查询的Mocha测试使用async/await挂起
有人能给我解释一下这段代码的工作原理吗:使用Node.js和Node postgresl更新查询的Mocha测试使用async/await挂起,node.js,typescript,node-postgres,Node.js,Typescript,Node Postgres,有人能给我解释一下这段代码的工作原理吗: const articleUpdated = async(pool: Pool, author: String): Promise<void> => { const query = { text: 'UPDATE articles SET end_time=NOW(), author=$1', values: [author], } pool.query(query) .then(result
const articleUpdated = async(pool: Pool, author: String): Promise<void> => {
const query = {
text:
'UPDATE articles SET end_time=NOW(), author=$1',
values: [author],
}
pool.query(query)
.then(result => { console.error('Test'); result.rows })
.catch(error => console.error(error))
}
const-articleUpdated=async(池:池,作者:字符串):Promise=>{
常量查询={
正文:
'UPDATE articles SET end_time=NOW(),author=$1',
值:[作者],
}
pool.query(查询)
.then(result=>{console.error('Test');result.rows})
.catch(错误=>console.error(错误))
}
当这个挂起时:
const articleUpdated = async(pool: Pool, author: String): Promise<void> => {
const query = {
text:
'UPDATE articles SET end_time=NOW(), author=$1',
values: ['test'],
}
let { rows } = await pool.query(query)
console.log(rows)
}
const-articleUpdated=async(池:池,作者:字符串):Promise=>{
常量查询={
正文:
'UPDATE articles SET end_time=NOW(),author=$1',
值:[“测试”],
}
让{rows}=wait pool.query(查询)
console.log(行)
}
上述代码在正常应用程序中运行时不会出现问题。然而,当我通过Mocha编写测试时,带有query/then/catch的第一个代码段工作正常,测试完成时没有问题。但是,如果我使用async/await将代码更改为snippet2,测试执行将挂起在await pool.query(query)
line
代码不是通过Mocha直接调用的,而是通过其他模块调用的,这些模块依次调用上述代码。我逐行遍历了测试,每次它挂起时,执行都会使用wait pool.query(query)
我的猜测是,可能是一些承诺没有返回或完成,或者更新查询没有更新任何记录。不确定
正如我前面提到的,上面的代码是通过集成测试调用的(在父方法上使用await
)。调用与运行应用程序时调用的方式完全相同
如有任何关于故障排除的提示、想法或建议,将不胜感激
提前谢谢