I am writing a test in Mocha / Node js and want to use setTimeout to wait for the period before the code block executes.
How can i do this?
It seems that in the test case Mocha setTimeout () does not work. (I know that you can set Timeout for each test case and for each test file, this is not what I need here.)
In js file with Node, i.e. node miniTest.js , this will wait 3 seconds and then print the line inside the setTimeout function.
miniTest.js
console.log('waiting 3 seconds...'); setTimeout(function() { console.log('waiting over.'); }, 3000);
In the js file launched with Mocha, i.e. mocha smallTest.js , it will not wait and complete execution and exit without printing a line inside the setTimeout function.
smallTest.js:
mocha = require('mocha'); describe('small test', function() { it('tiny test case', function() { console.log('waiting 3 seconds...'); setTimeout(function () { console.log('waiting over.') }, 3000); }); });
source share