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

Created on 19 Mar 2013  ·  17Comments  ·  Source: mochajs/mocha

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

Most helpful comment

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

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

All 17 comments

+1

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

Hello, are you there? Can someone fix this? I found out two places in code where this is problem, probably - timeout methods calling other timeout methods, but not doing apply(this, arguments) but passing the first one by hand, thus killing the if length === 0 readTheValue semantics.

As the system is pretty complex, you know better if adding apply there is enough, if yes, the fix should be easy.

+1

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)

@travisjeffery,

I voted for this

I have the same problem with the "before all" hook.

all

before(function(){
  this.timeout(500);
})

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

  it('should take less than 500 too', function(done){
    setTimeout(done, 200);
  });
})
beforeEach(function(){
  this.timeout(500);
})

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

  it('should take less than 500 too', function(done){
    setTimeout(done, 200);
  });
})
describe('something', function(){  
  beforeEach(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);
  });
})

returns this

mocha --version
   1.20.1

mocha --timeout 100

  something
    1) should take less than 500
    2) should take less than 500 too


  0 passing (215ms)
  2 failing

  1) something should take less than 500:
     Error: timeout of 100ms exceeded

  2) something should take less than 500 too:
     Error: timeout of 100ms exceeded

my expectations that 500 should be applied for each test

Should I create separate issue?

ok i'm gonna close this then. hopefully you can look into making a pr rather than an issue for the befores.

Please reopen, this.timeout() still fails, see https://github.com/visionmedia/mocha/blob/master/lib/context.js#L39 calling directly with ms argument thus making length of arguments always 1 in versus https://github.com/visionmedia/mocha/blob/master/lib/runnable.js#L66 which can never be true and return the value.

the case where that would fail is if you're getting the value of the timeout not setting it

Yes, that's what I had a problem with in the first place.

"Dr. Travis Jeffery" [email protected]ísal/a:

thecasewherethatwouldfailisifyou'regettingthevalueofthetimeoutnotsettingitReplytothisemaildirectlyorviewitonGitHub.

@herby fixed in #1282

@travisjeffery thanks

fyi, still seeing this in 3.1.2

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

@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.

@AgentME: You're right, sorry. In my haste I didn't see the other issue that pointed out this fact.

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

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

@travisjeffery i used the below script but i got the same Timeout exceed error.

Myscript :

describe("getBillingDetail", async function (){
this.timeout(55000);
it.only("check given valid Jobname",async function (done){
this.timeout(55000);
var result = await url.getBillingDetail('12254785565647858');
console.log(result);
assert.equal(result,true);
});
});

Error: Timeout of 55000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Aarbel picture Aarbel  ·  3Comments

CADBOT picture CADBOT  ·  3Comments

niftylettuce picture niftylettuce  ·  3Comments

danielserrao picture danielserrao  ·  3Comments

luoxi001713 picture luoxi001713  ·  3Comments