【发布时间】:2017-08-20 07:14:43
【问题描述】:
我有一个类似于下面显示的测试。基本上我想测试一个特定的方法是否被延迟。
以下示例按预期工作,即调用 resolve 方法并且测试通过:
it(`should delay execution by 1 second`, function () {
const clock = sandbox.useFakeTimers();
const p = new Promise(function (resolve) {
setTimeout(resolve, 1000);
});
clock.tick(1000);
return p;
});
但是,如果我将 setTimeout 包装在另一个 Promise 中,则永远不会调用 resolve:
it(`should delay execution by 1 second`, function () {
const clock = sandbox.useFakeTimers();
const p = Promise.resolve()
.then(() => {
return new Promise(function (resolve) {
setTimeout(resolve, 1000); // resolve never gets called
});
});
clock.tick(1000);
return p;
});
这里有什么问题?
我在 Node 6.9.5 上使用 Sinon 2.1.0 和原生承诺。
【问题讨论】:
标签: javascript node.js promise settimeout sinon