如何在打字稿测试中增加每个套件的摩卡超时

How to increase the mocha timeout per suite in a typescript test

我正在尝试增加 mocha 测试的超时时间,因为它们是构成自动化 UI 测试套件一部分的 Web 请求,因此可能需要比默认的 2000 毫秒更长的时间。

如果我调用 mocha 并将 --timeout 设置为 5000 毫秒左右,但默认的 2000 毫秒是不够的,代码本身就可以正常工作。

我希望能够为每个测试套件设置超时,以便超时成为成功标准的一部分,这可能因具体情况而异。

before(()=>{
  var sw = require('selenium-webdriver');
  this.driver = new sw.Builder().withCapabilities(sw.Capabilities.chrome()).build();
  var c = require('chai');
  c.use(require('chai-webdriver')(this.driver));
  this.expect = c.expect;
  return this.driver.getWindowHandle();
})

after(() => {
  return this.driver.quit();
})

describe('Looking at github', () => {
  beforeEach(() => {
    this.driver.get('http://whosebug.com/');
  })
  describe('When we take a look at the stack overflow home page', () => {
    return it('It does not have crazy cat text in it!', () => {
      return this.expect('#h-top-questions').dom.to.not.contain.text("Just cats here!");
    });
  });
})

使用 [=h11=] 而不是 arrow 然后调用 this.timeout(5000); 例如

describe('When we take a look at the stack overflow home page', () => {
    return it('It does not have crazy cat text in it!', function() {
      this.timeout(5000);
      return this.expect('#h-top-questions').dom.to.not.contain.text("Just cats here!");
    });
  });

这是因为()=>捕捉到了周围的this。更多 http://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

basarat 答案的替代方案,使用不同语法的类似线路(以不同的方式有效混淆!):

describe('When we take a look at the stack overflow home page', () => {
    it('does not have crazy cat text in it!', () => {
        expect('#h-top-questions').dom.to.not.contain.text("Just cats here!");
    }).timeout(5000);
});