【发布时间】:2023-04-03 16:55:02
【问题描述】:
Qunit 会一一执行异步测试,但它如何知道测试已完成,因为测试没有返回 qunit 可以等待的 Promise?
在这个演示示例中https://jsfiddle.net/6bnLmyof/
function squareAfter1Second(x) {
const timeout = x * 1000;
console.log("squareAfter1Second x:", x);
return new Promise(resolve => {
setTimeout(() => {
resolve(x * x);
}, timeout);
});
}
const { test } = QUnit;
test( "an async test", async t => {
console.log("starting test1");
t.equal( await squareAfter1Second(3), 9 );
t.equal( await squareAfter1Second(4), 16 );
});
test( "an async test2", async t => {
console.log("starting test2");
t.equal( await squareAfter1Second(1), 1 );
});
有 2 个异步测试一个一个地运行。测试将宏任务 (setTimeout) 发布到事件循环,但不知何故 qunit 能够等待测试完成,尽管测试没有返回承诺。另外,qunit 的源代码中没有 await 关键字。
【问题讨论】:
标签: javascript es6-promise qunit