设置超时箭头功能

时间:2018-08-27 17:19:02

标签: node.js mocha

我正在做一些摩卡测试,我被要求重构我的代码,他们要求我使用箭头功能。

但是现在出现以下错误:

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

这也发生在重构之前,但是我使用了this.timeout(1000)来解决它,但是现在这不适用于箭头功能。如何设置超过2000ms的超时时间?下面是我的测试。

describe('Test', () => {
  token = 'un_assigned';
  before( (done) => {
    getToken('random_token', (response) => {
      token = response.token;
      fs.writeFileSync('./tests/e2e/helpers/token.json', JSON.stringify(response, null, 4));
      done();
    })
  });

  files.forEach(function (file) {
    it('Comparando file ' + file, (done) => {
      const id = file.split('./screenshots/')[1];
      compare(file, id, token, function (response) {
        expect(response.TestPassed).to.be.true;
        done();
      });
    });
  });
});

2 个答案:

答案 0 :(得分:1)

使用箭头函数时未绑定测试上下文。因此,如果要使用this.timeout

,则无法使用箭头功能

但是您可以这样设置特定测试用例的超时时间:

it('Comparando file ' + file, (done) => {
  ...
}).timeout(1000);

答案 1 :(得分:0)

我也遇到了这个问题,并通过将函数转换为以下格式来解决了这个问题:

it('should do the test', done => {

    ...

    done();
}.timeout(15000);

只有在我同时加入了两者 done()timeout(15000)之后,错误消失了(当然,15000可以比程序运行时长任何数目)。

请注意,尽管此快速修复方法对我有用,因为我不为缓慢所困扰,但通常来说,通过延长超时时间来修复此类错误是错误的编码做法,而应该提高程序速度。

相关问题