【发布时间】:2019-08-21 22:09:00
【问题描述】:
我正在使用 mocha 框架在 nodejs 中编写测试。由于我正在测试的端点是异步的,因此我使用了 aync-await 概念。但是测试用例不等待 before() 执行部分完成运行,即;异步函数,因此显示 listAll() api 的错误结果。
async function fetchContent() {
const [profile, user] = await Promise.all([api.profiles.list(), api.users.list()])
params = {userId: user.items[0].id, label: 'Test', profileId: profile.items[0].id, token: authToken}
testApi = new Api(params)
testApi.profiles.create(params)
}
before(async () => {
await fetchContent()
})
describe('Profiles API', () => {
it('list profiles', done => {
testApi.profiles.listAll().then(response => {
console.log('list=', response)
})
done()
})
})
我也尝试了 it(),如下所示,但 listAll() 仍然不显示作为 before() 执行的一部分创建的配置文件记录:
describe('Profiles API', () => {
it('list profiles', async () => {
const response = await testApi.profiles.listAll()
console.log('list=', response)
})
【问题讨论】:
-
函数
fetchContent内部最后一次调用testApi.profiles.create(params)是异步的吗? -
@SamuelVaillant 是的 create() 是异步的,listAll() in it() test 也是如此。
标签: node.js async-await mocha.js