Mocha单元测试Jenkins的超时错误,但不在本地测试

时间:2017-09-13 01:06:15

标签: javascript unit-testing jenkins mocha

我正在研究的这个项目有很多单元测试,Jenkins正在检查项目构建,并运行这些测试。

我遇到的问题是:每当我在本地运行测试时,都不会出现错误。一切正常,无论是单独测试文件还是在整个项目上运行测试。

但是,每当我推送任何提交时,Jenkins总会返回此错误:

timeout of 20000ms exceeded. Ensure the done() callback is being called in this test.

我不知道为什么,因为当我在本地测试时它可以工作,我确信我在测试中调用了done()。此外,它始终是相同的两个单元测试,它们在Jenkins上一直失败并且在我们的任何一台机器上从未在本地出现过。

测试看起来像这样:

it('should provide result', function(done) {
  this.timeout(20000);
  server.get(`/api/model/function/${token}`)
  .set('Authorization', access_token)
  .expect(200)
  .end((err, res) => {
    assert.equal(res.status, 200, 'expects status equal to 200');
    done(err);
  });
});

我也尝试将其重写为看似

的承诺
it('should provide result', function(done) {
  server.get(`/api/model/function/${token}`)
  .set('Authorization', access_token)
  .then(res => {
    assert.equal(res.status, 200, 'expects status equal to 200');
    done();
  });
});

结果相同:在本地工作正常但在Jenkins中没有。

我环顾四周,看到有人建议在最后添加.catch(done).catch(err => { done(err) }),但这对我来说只会导致错误Done() called multiple times

非常感谢有关此事的任何帮助。

1 个答案:

答案 0 :(得分:0)

对于不同的"系统并不常见。在网络连接等方面具有不同的特性。有时您只需要填充它并更改CI环境中特定测试的超时。因此,只需为你的失败测试提高它:

describe("something", function () {
    this.timeout(5000);

    // tests...
});

但是,根据您在聊天中所说的内容,听起来服务器并未在CI环境中进行调整。请将代码发布到设置服务器变量的位置。