【发布时间】:2019-08-26 05:16:45
【问题描述】:
所以,我有这样的测试:
it 'sample test', (done)->
await Promise.resolve 0
Promise.resolve 0
.then ->
done()
null
注意,null 到底是为了避免返回 Promise。
但是,测试属于经典"Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both"
我检查了结果JS代码,没有什么奇怪的:
it('Sample test', async function(done) {
await Promise.resolve(0);
Promise.resolve(0).then(function() {
return done();
});
return null;
});
我不明白,出了什么问题,因为(我认为)这段代码不应该返回承诺。
另外,当我将第一个承诺(使用await)包装到setTimeout 中时,它工作正常。
it 'working test', (done)->
setTimeout ->
await Promise.resolve 0
, 0
Promise.resolve 0
.then ->
done()
null
当然,使用setImmediate 而不是setTimeout 它也可以,所以我认为,在这种情况下,治疗方法是回调。但这是非常肮脏的解决方案。
如何在一个测试中更清晰地混合then、async-await和done?
【问题讨论】:
标签: javascript async-await coffeescript mocha.js es6-promise