[英]How to use async-await and then in one Mocha test with done?
So, I have test like this: 因此,我进行了如下测试:
it 'sample test', (done)->
await Promise.resolve 0
Promise.resolve 0
.then ->
done()
null
Notice, that null
in the end is to avoid returning Promise. 注意,最后的
null
是为了避免返回Promise。 However, test falls with classic "Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both"
但是,测试属于经典的
"Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both"
I checked result JS code, nothing strange: 我检查了结果JS代码,没什么奇怪的:
it('Sample test', async function(done) {
await Promise.resolve(0);
Promise.resolve(0).then(function() {
return done();
});
return null;
});
I don't understand, what is wrong, because (as I think) this code should not return promise. 我不明白这是怎么回事,因为(按照我的想法)此代码不应返回promise。 Also, when I wrapped first promise (with
await
) into setTimeout
, it works fine. 另外,当我将第一个承诺(带有
await
)包装到setTimeout
,它可以正常工作。
it 'working test', (done)->
setTimeout ->
await Promise.resolve 0
, 0
Promise.resolve 0
.then ->
done()
null
Of course, with setImmediate
instead of setTimeout
it works too, so I think, that the cure in this situation is callback. 当然,使用
setImmediate
代替setTimeout
也可以,所以我认为这种情况下的解决方法是回调。 But this is extremely dirty solution. 但这是极其肮脏的解决方案。 How to mix
then
, async-await
and done
in one test more clearly? then
如何在一个测试中更清晰地混合, async-await
和done
?
Using await
in the function body turns the test function into an async
function. 在函数体中使用
await
将测试函数转换为async
函数。
async
functions always return a Promise
. async
函数总是返回Promise
。
So in this case: 因此,在这种情况下:
it('Sample test', async function(done) {
await Promise.resolve(0);
Promise.resolve(0).then(function() {
return done();
});
return null;
});
...the test function is returning a Promise
that will resolve to null
. ...测试函数返回的
Promise
将解析为null
。
In your other example Mocha doesn't complain because the code compiles to this: 在您的另一个示例中,Mocha不会抱怨,因为代码编译为此:
it('working test', function(done) {
setTimeout(async function() {
return (await Promise.resolve(0));
}, 0);
Promise.resolve(0).then(function() {
return done();
});
return null;
});
...since await
is now within the body of the function passed to setTimeout
. ...因为现在
await
在传递给setTimeout
的函数体内。
(Note that the two tests behave very differently ). (请注意,这两个测试的行为非常不同 )。
There isn't a reason to use both done
and an async / await
test function (or one that returns a Promise
) so Mocha fails the test with that error. 没有理由同时使用
done
和async / await
测试函数(或返回Promise
函数),因此Mocha失败并显示该错误。
Your first test can be simplified to this: 您的第一个测试可以简化为:
it 'sample test', ()->
await Promise.resolve 0
await Promise.resolve 0
...or if you need to do work in a then
chained to the second Promise
you can just do this: ...或者如果您需要在第二个
Promise
进行工作, then
执行以下操作:
it 'sample test', ()->
await Promise.resolve 0
await Promise.resolve 0
.then ->
// do stuff here
In Mocha v3.0.0 and newer, returning a Promise and calling done()
will result in an exception, as this is generally a mistake - docs 在Mocha v3.0.0及更高版本中,返回Promise并调用
done()
将导致异常,因为通常这是一个错误-docs
Since async function
always return Promise
you are getting this error. 由于
async function
总是返回 Promise
,因此会出现此错误。 Possible solutions: 可能的解决方案:
Remove async function
删除
async function
it('Sample test', function(done) { Promise.resolve(0) .then(function() { ... }) .then(function() { ... // if necessary }) .then(function() { done(); }); });
Return Promise
退货
Promise
it('Sample test', function() { return Promise.resolve(0) .then(function() { ... }) .then(function() { ... // if necessary }); });
Use async/await
使用
async/await
it('Sample test', async function() { await Promise.resolve(0); await Promise.resolve(0); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.