Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to increase the timeout for mocha tests as they are web requests that form part of an automated UI test suite and therefore can take longer than the default 2000ms.

The code itself works great if I call mocha with the --timeout set to 5000ms or so but the default 2000ms is not enough.

I want to be able to set the timeout per test suite so that the timeout becomes part of the success criteria which might be different on a case by case basis.

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://stackoverflow.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!");
    });
  });
})
like image 453
Ed Bishop Avatar asked Jul 14 '15 21:07

Ed Bishop


People also ask

How do I increase my Mocha timeout?

Whenever you run Mocha at the command line, it will read this file and set a timeout of 5 seconds by default. Another way which may be better depending on your situation is to set it like this in a top level describe call in your test file: describe("something", function () { this. timeout(5000); // tests... });

How do you skip the Mocha test?

This inclusive ability is available in Mocha by appending . skip() to the suite or to specific test cases. The skipped tests will be marked as "pending" in the test results.

What is it in Mocha?

Mocha is a feature-rich JavaScript test framework running on Node. js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.


2 Answers

Use function intead of an arrow and then just call this.timeout(5000); e.g.

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 is because ()=> captures the surrounding this. More http://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

like image 196
basarat Avatar answered Oct 17 '22 01:10

basarat


An alternative to basarat's answer, along similar lines that uses different syntax (that is effectively confusing in a different way!):

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);
});
like image 32
alwaysmpe Avatar answered Oct 16 '22 23:10

alwaysmpe