【问题标题】:How to get rid of timeout error in mocha-chai test despite using done() for async call?尽管使用 done() 进行异步调用,如何摆脱 mocha-chai 测试中的超时错误?
【发布时间】:2018-08-19 18:24:55
【问题描述】:

我在 mocha 测试套件中使用 setTimeout 插入 20 秒延迟,然后在 describe 块中对 it() 进行最后一次 post 调用。虽然,我正在使用 done() ,但我在终端上仍然出现以下错误:

错误:超过 2000 毫秒的超时。对于异步测试和钩子,确保调用“done()”;如果返回一个承诺,请确保它解决错误:超过 2000 毫秒的超时。对于异步测试和钩子,确保调用“done()”;如果返回一个承诺,请确保它解决

我做错了什么?

下面是我的代码:

describe('Testing get and post APIs', ()=> {

            it('Series of get and post', (done) => {
                chai.request(server)
                .post('/thisis/1st_post')
                .send()
                .end((err, res) => {
                 expect(res.statusCode).to.equal(200);                                 

                 chai.request(server)
                .get('/thisis/1st_get')
                .send()
                .end((err, res) => {
                 expect(res.statusCode).to.equal(200);
                 setTimeout(function() {
                       chai.request(server)
                      .post('/thisis/last_post')
                      .send()
                      .end((err, res) => {
                      expect(res.statusCode).to.equal(200); 
                      done();
                 })
                 },20000);  
               }); 
             });     
        });
});

谢谢。

【问题讨论】:

标签: mocha.js settimeout chai


【解决方案1】:

如果您希望测试运行器等待的时间超过默认时间,您需要更改超时值。例如,尝试将其添加到 describe 块的开头:

this.timeout(30 * 1000); // wait up to 30 seconds before failing from timeout

另见:Change default timeout for mocha & https://mochajs.org/api/test#timeout

但是,根据您希望延迟 20 秒的原因,延长测试超时可能是错误的解决方案。如果您使用延迟只是为了处理请求承诺永远不会解决的情况,那么更好的方法是使用Promise.race。如果不进一步了解您的情况,我很难判断。

【讨论】:

    【解决方案2】:

    超时设置为20000 (20 seconds),但基于错误的测试超时为2000 (2 seconds)。这意味着我们需要为测试本身设置更大的超时时间。

    describe('Testing get and post APIs', function() { // don't use () because we want to use `this` in the next line
      this.timeout(40000); // set timeout here
    
      it('Series of get and post', function(done) {
        chai.request(server)
          .post('/thisis/1st_post')
          .send()
          .end((err, res) => {
            expect(res.statusCode).to.equal(200);
    
            chai.request(server)
              .get('/thisis/1st_get')
              .send()
              .end((err, res) => {
                expect(res.statusCode).to.equal(200);
                setTimeout(function () {
                  chai.request(server)
                    .post('/thisis/last_post')
                    .send()
                    .end((err, res) => {
                      expect(res.statusCode).to.equal(200);
                      done();
                    })
                }, 20000);
              });
          });
      });
    });
    

    我想知道我们是否可以进行如下测试。它更干净,更易于维护。

     describe('Testing get and post APIs', function () { // don't use () because we want to use `this` in the next line
      this.timeout(40000); // set timeout here
    
      it('Series post', function () { // no need done() because we can just return promise
        return chai.request(server)
          .post('/thisis/1st_post')
          .send()
          .end((err, res) => {
            expect(res.statusCode).to.equal(200);
          })
      });
    
      it('Series get', function () {
        return chai.request(server)
          .get('/thisis/1st_get')
          .send()
          .end((err, res) => {
            expect(res.statusCode).to.equal(200);
          });
      });
    
      it('Series last post', function(done) {
        setTimeout(function () {
          chai.request(server)
            .post('/thisis/last_post')
            .send()
            .end((err, res) => {
              expect(res.statusCode).to.equal(200);
              done();
            });
        }, 20000);
      });
    });
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-06
      • 2013-11-23
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 2017-12-25
      • 2020-10-23
      相关资源
      最近更新 更多