摩卡咖啡-等待一分钟后再执行测试

时间:2018-11-22 15:39:43

标签: javascript node.js mocha

我遇到这样的情况,测试需要等待一分钟才能执行。尝试使用以下代码,但无法正常工作:

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,但没有用。

如何在执行测试之前等待一分钟?

1 个答案:

答案 0 :(得分:2)

如果使用了承诺,则最好不要将它们与普通的回调函数混合使用。

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(...);
    ...
  });