mocha: this.timeout() inside describe() doesn't work for me

This below JS snippet is from the mocha.js website docs, “Suite specific timeouts”:

describe('a suite of tests', function(){
  this.timeout(500);

  it('should take less than 500ms', function(done){
    setTimeout(done, 300);
  })

  it('should take less than 500ms as well', function(done){
    setTimeout(done, 200);
  })
})

This code example is a bit confusing, when I put the “this.timeout()” call like this I get:

C:\Dev\GitHub\alexlatchford\adfuser\src\test\api\v1\target_groups.js:274
                this.timeout(0); // Extend the timeout for this suite because we're insertin
                     ^
TypeError: Object #<Object> has no method 'timeout'
    at C:\Dev\GitHub\alexlatchford\adfuser\src\test\api\v1\target_groups.js:274:8
    at module.exports.suite.on.context.describe.context.context (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\interfaces\bdd.js:72:7)
    at C:\Dev\GitHub\alexlatchford\adfuser\src\test\api\v1\target_groups.js:272:2
    at module.exports.suite.on.context.describe.context.context (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\interfaces\bdd.js:72:7)
    at Object.<anonymous> (C:\Dev\GitHub\alexlatchford\adfuser\src\test\api\v1\target_groups.js:15:1)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Mocha.loadFiles (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:137:27)
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:134:14)
    at Mocha.run (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:278:31)
    at Object.<anonymous> (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:324:7)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:245:9)

It works if I put it inside my before() for that group of tests, which incidentally was what I intended anyway, but I think either the docs need clarifying or the error message investigating 😃

Cheers, Alex

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 17 (4 by maintainers)

Commits related to this issue

Most upvoted comments

For posterity, you can use arrow functions and set the timeout like this:

it('some test', (done) => {
    // ...
}).timeout(5000);

i think i recently fixed this. your code worked for me too:

describe('something', function(){
  this.timeout(500);

  it('should take less than 500', function(done){
    setTimeout(done, 300);
  });

  it('should take less than 500 too', function(done){
    setTimeout(done, 200);
  });
})
🍕  mocha


  something
    ✓ should take less than 500 (301ms)
    ✓ should take less than 500 too (201ms)


  2 passing (510ms)

@JacobRodriguezSSI By using an arrow function, you’re not using the this value passed to describe’s callback. You should use a normal function instead.

fyi, still seeing this in 3.1.2

describe('something', () => {
  this.timeout(5000);
  it('should work', done => {
    setTimeout(done, 2000);
  });
});

I have the same problem with the “before all” hook. It graps the timeout that I specified globally in stead of the one in the function with this.timeout(2000);