title: How can I use setTimeout() functions within Mocha test cases?
body: <p>I'm writing a test in Mocha / Node js and want to use a setTimeout to wait for a period before executing a block of code.</p>
  
  <p>How can I accomplish this?</p>
  
  <p>It appears that within a Mocha test case, setTimeout() does not work. (I am aware that you can setTimeout per test case and per test file, that's not what I need here.)</p>
  
  <p>In a js file run with Node, ie, <code>node miniTest.js</code>, this will wait 3 seconds, then print the line inside the setTimeout function.</p>
  
  <h2>miniTest.js</h2>
  
  <pre><code>console.log('waiting 3 seconds...');
  setTimeout(function() {
      console.log('waiting over.');
  }, 3000);
  </code></pre>
  
  <p>In a js file run with Mocha, ie, <code>mocha smallTest.js</code>, this will not wait, and will finish executing and exit without ever printing the line inside the setTimeout function.</p>
  
  <h2>smallTest.js:</h2>
  
  <pre><code>mocha = require('mocha');
  
  describe('small test', function() {
      it('tiny test case', function() {
          console.log('waiting 3 seconds...');
          setTimeout(function () {
              console.log('waiting over.')
          }, 3000);
      });
  });
  </code></pre>