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)发布到事件循环中,但是尽管测试未返回promise,但qunit仍能够以某种方式等待测试完成。同样,在qunit的源代码中,不存在等待关键字。