There is one long test, after which it is necessary to clean up the tracks. I use an after hook, but the execution time is longer than the standard 2000ms, which is why the whole test failed.

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

For tests it is possible to change this timeout , but with after it does not work. I have a feeling that I missed something very simple ..

UP:

I made an example that looks more like my real test and which can reproduce the problem:

 function long_async_function(cb){ setTimeout(cb,3000); } function clear_test_data(cb){ describe('clearing test', function() { this.timeout(3333); it('clear data and test it',function(done){ long_async_function(function(){ done(); console.log('Clearing test call after callback'); cb(); }); }); }); } describe('long test', function() { this.timeout(3333); it('do something',function(done){ long_async_function(done); }); after(function(done){ this.timeout(12000); clear_test_data(done); }); }); 

In this form, the test is performed 18 seconds. If you change the timeout in the after, the test execution time changes proportionally. After hook causes fails of the entire test due to exceeding the timeout, but at the same time, as the console shows, its done is invoked, but after an error with a timeout. What did I do wrong?

  • And how exactly did you change the timeout? Can you provide a minimal piece of code to illustrate the problem? - Dmitriy Simushev
  • Of course, now bring. It's just that everything can be reproduced using examples from the documentation - Darth
  • My answer is almost ready. But it essentially depends on how exactly you changed the timeout :) - Dmitriy Simushev
  • Here, I gave an example (I wrote to the eye, because my test is very long and it would take longer to bring its pieces) - Darth
  • And for what you do describe in the after hook. Brad of some sort - Dmitriy Simushev

1 answer 1

Your problem is that you indirectly use the describe in hooks ( after , before and others). So it is absolutely impossible to do.

Trying to guess why you are getting the test execution timeout is pointless, because You are not working properly with the test framework. Mocha does n't work that way !

The correct solution to the problem is a complete rewriting of tests with the separation of the code for the tests themselves ( describe + it ) and the code for the initialization / finalization of the environment (hooks after , before , ...).

And yes, the problem has nothing to do with this.timeout / this.slow , since these functions work fine.