我遇到测试需要等待一分钟才能执行的情况。尝试下面的代码,但不起作用:
describe('/incidents/:incidentId/feedback', async function feedback() {
it('creates and update', async function updateIncident() {
// this works fine
});
// need to wait here for a minute before executing below test
it('check incident has no feedback', function checkFeedback(done){
setTimeout(function(){
const result = send({
user: 'Acme User',
url: `/incidents/${createdIncident.id}/feedback`,
method: 'get',
});
console.log(result);
expect(result.response.statusCode).to.equal(200);
expect(result.response.hasFeedback).to.equal(false);
done();
}, 1000*60*1);
});
});
在这里,
send()
返回Promise
。我试着用async await
,但没有奏效。如何在执行前等待测试一分钟?
最佳答案
如果使用了promise,最好不要将它们与普通回调混合使用。
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
...
it('check incident has no feedback', async function checkFeedback(){
this.timeout(1.33 * 60 * 1000);
await wait(1 * 60 * 1000);
const result = await send(...);
...
});
关于javascript - Mocha -等待一分钟后再执行测试,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53434297/