我有以下测试用例:
it("should pass the test", async function (done) {
await asyncFunction();
true.should.eq(true);
done();
});
运行它断言:
错误:解决方法指定过多。指定回调or返回一个 Promise;不是都。
如果我删除done();
声明,它断言:
错误:超时超过 2000 毫秒。对于异步测试和挂钩,请确保
调用“done()”;如果返回 Promise,请确保它得到解决。
如何解决这个悖论呢?
您需要删除done
参数也是如此,而不仅仅是对它的调用。像 Mocha 这样的测试框架会查看函数的参数列表(或至少其数量)来了解您是否正在使用done
或类似的。
使用 Mocha 3.5.3,这对我有用(必须更改true.should.be(true)
to assert.ok(true)
因为前者抛出了错误):
const assert = require('assert');
function asyncFunction() {
return new Promise(resolve => {
setTimeout(resolve, 10);
});
}
describe('Container', function() {
describe('Foo', function() {
it("should pass the test", async function () {
await asyncFunction();
assert.ok(true);
});
});
});
但如果我添加done
:
describe('Container', function() {
describe('Foo', function() {
it("should pass the test", async function (done) { // <==== Here
await asyncFunction();
assert.ok(true);
});
});
});
...然后我明白了
错误:超时超过 2000 毫秒。对于异步测试和挂钩,确保调用“done()”;如果返回 Promise,请确保它得到解决。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)