0
我正在学习使用最新的ecmascript语法对我的MongoDB后端代码进行笑话测试。我现在正在测试如果我试图从空集合中找到文档,那么测试是否会通过测试。nodejs异步/等待尝试/捕获笑话测试通过时不应该
光标应该null
,结果因为没有返回,这意味着光标falsey,但是当我告诉它期望truthy,我不知道下面还测试通过,甚至为什么:
import config from './config'
const mongodb = require('mongodb')
it('sample test',() => {
mongodb.MongoClient.connect(config.mongodb.url, async (connectErr, db) => {
expect(db).toBeTruthy()
let cursor
try {
cursor = await db.collection('my_collection').findOne()
// cursor is null, but test still passes below
expect(cursor).toBeTruthy()
} catch (findErr) {
db.close()
}
})
})
此外,这是一个很好的测试测试风格?我在某处读过,你不应该在测试中使用try/catch块。但是,这是你将用来处理异步/等待错误。