elcharrua
elcharrua

Reputation: 1682

Set timeout arrow functions

Im working on some mocha tests and I was asked to refactor my code in which they ask me to use arrow functions.

But now I'm getting the following error:

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

This also happened when before the refactor but to solve it I used: this.timeout(1000) but now this doesn't work with arrow functions. How could I set a timeout higher than 2000ms ? Below Is my test.

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();
      });
    });
  });
});

Upvotes: 1

Views: 929

Answers (2)

half of a glazier
half of a glazier

Reputation: 2076

I had this problem as well, and fixed it by converting my function to this format:

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

    ...

    done();
}.timeout(15000);

The error only went away after I included both the done() and the timeout(15000) (course, the 15000 can be any number longer than the program runtime).

Note that although this quick fix worked for me because I'm not bothered by slowness, as a general rule it's bad coding practice to fix errors like this by extending timeout, rather the program speed should be improved.

Upvotes: 1

Sasha Pomirkovany
Sasha Pomirkovany

Reputation: 542

Test context isn't being bound when using arrow functions. So you can't use this.timeout.

But you can set timeout on specific test cases this way:

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

Upvotes: 3

Related Questions