【问题标题】:mocha - wait for a minute before executing testmocha - 在执行测试前等待一分钟
【发布时间】:2019-04-25 08:02:03
【问题描述】:

我有一种情况,测试需要等待一分钟才能执行。尝试了下面的代码,但它不起作用:

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

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

【问题讨论】:

  • 如果您正在等待从另一个测试中更新某些内容,那么您做错了。

标签: javascript node.js mocha.js


【解决方案1】:

如果使用了 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(...);
    ...
  });

【讨论】:

  • 试过这个。它会引发错误Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
  • 这是一个不同的问题。这意味着您需要在使用如此长的延迟之前增加超时。请参阅stackoverflow.com/questions/15971167/…
猜你喜欢
  • 2014-10-21
  • 2020-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 2021-01-23
相关资源
最近更新 更多