Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout on before hook in mocha?

Tags:

mocha.js

I want to set timeout value on before hook in mocha test cases. I know I can do that by adding -t 10000 on the command line of mocha but this will change every test cases timeout value. I want to find a way to change the timeout programmatically below is my code:

describe('test  ', () => {    before((done) => {         this.timeout(10000);          ... 

it will complain about the line this.timeout(1000) that timeout is not defined. How to set the timeout on before hook.

like image 572
Joey Yi Zhao Avatar asked Jan 31 '17 04:01

Joey Yi Zhao


People also ask

How do I change the default timeout on my Mocha?

If you (like me) use a script in your package. json file, just add the --timeout option to mocha: "scripts": { "test": "mocha 'test/**/*. js' --timeout 10000", "test-debug": "mocha --debug 'test/**/*.


2 Answers

You need to set a timeout in your describe block rather than in the hook if you want it to affect all the tests in the describe. However, you need to use a "regular" function as the callback to describe rather than an arrow function:

describe('test', function () {   this.timeout(10000);    before(...);    it(...); }); 

In all places where you want to use this in a callback you pass to Mocha you cannot use an arrow function. You must use a "regular" function which has its own this value that can be set by Mocha. If you use an arrow function, the value of this won't be what Mocha wants it to be and your code will fail.

You could set a different timeout for your before hook but there are two things to consider:

  1. Here too you'd need to use a "regular" function rather than an arrow function so:
before(function (done) {    this.timeout(10000); 
  1. This would set a timeout only for the before hook and would not affect your tests.
like image 108
Louis Avatar answered Sep 22 '22 13:09

Louis


as instructed at https://mochajs.org/#hook-level, you have to use a regular function call to set the timeout.

before(function(done) {   this.timeout(3000); // A very long environment setup.   setTimeout(done, 2500); }); 

if you insist on use arrow or async function in the hook. You may do it this way:

before(function (done) {   this.timeout(3000);   (async () => {     await initilizeWithPromise();   })().then(done); }); 

It is quite helpful and nice-looking if you got multiple async calls to be resolved in the hook.

updated: fuction def works well with async too. So this hook can be upgraded to

before(async function () {   this.timeout(3000);   await initilizeWithPromise();       }); 

So it provides benefits from both this and await.

By the way, mocha works pretty fine with promises nowadays. If timeout is not a concern. Just do this:

before(async () => {   await initilizeWithPromise(); }); 
like image 44
aGuegu Avatar answered Sep 22 '22 13:09

aGuegu