title: Using a for loop in a Mocha Test
body: <p>I'm new to mocha, and I wanted to try using a for loop to create test cases.  I want to test a function that I've made that takes an input of standard 12 hour time, and outputs it into 24 hour military time.  This is what it looks like.</p>
  
  <pre><code>exports.main = function(time) {
  var hr = parseInt(time.substr(0,2));
  var period = time.substr(8,10);
  if (period == 'AM' &amp;&amp; hr == 12) {
      hr = '0';
  }
  if (period == 'PM' &amp;&amp; hr &lt; 12) {
             hr += 12;
  }
      hr.toString();
      if (hr &lt; 10) {
          hr = '0' + hr;
      }
   return time = time.replace(/^\d{2}/g, hr).substr(0,8);
  
  }
  </code></pre>
  
  <p>To test my function in mocha, I have two arrays, one array holds the standard times and the other holds the corresponding expected output.  I want to iterate through them and produce a test case for each iteration and test my function.  </p>
  
  <pre><code>test_array = ["12:00:00AM", "01:00:00AM", "02:00:00AM", "03:00:00AM", "04:00:00AM",
  "05:00:00AM", "06:00:00AM", "07:00:00AM", "08:00:00AM", "09:00:00AM",
  "10:00:00AM", "11:00:00AM", "12:00:00PM", "01:00:00PM", "02:00:00PM",
  "03:00:00PM", "04:00:00PM", "05:00:00PM", "06:00:00PM", "07:00:00PM",
  "08:00:00PM", "09:00:00PM", "10:00:00PM", "11:00:00PM"];
  
  against = ["00:00:00", "01:00:00", "02:00:00", "03:00:00", "04:00:00",
  "05:00:00", "06:00:00", "07:00:00", "08:00:00", "09:00:00", "10:00:00",
  "11:00:00", "12:00:00", "13:00:00", "14:00:00", "15:00:00", "16:00:00",
  "17:00:00", "18:00:00", "19:00:00", "20:00:00", "21:00:00", "22:00:00",
  "23:00:00"]
  </code></pre>
  
  <p>This is what my test script looks like:</p>
  
  <pre><code>var converter = require('../modules/time.js');
  describe('Time Converter', function() {
   describe('main()', function() {
    for(i = 0; i &lt; 24; i++) {
     it(test_array[i]  + ' should convert to ' + against[i], function() {
     var test = converter.main(test_array[i]);
     assert.equal(test, against[i]);
     });
    }
   });
  });
  </code></pre>
  
  <p>The following is the results of the tests:</p>
  
  <pre><code>0 passing (23ms)
  24 failing
  
  1) Time Converter main() 12:00:00AM should convert to 00:00:00:
   TypeError: Cannot read property 'substr' of undefined
    at Object.exports.main (app/modules/time.js:43:27)
    at Context.&lt;anonymous&gt; (app/test/test.js:35:26)
  
  2) - 24) has the same result:
  
  24) Time Converter main() 11:00:00PM should convert to 23:00:00:
   TypeError: Cannot read property 'substr' of undefined
    at Object.exports.main (app/modules/time.js:43:27)
    at Context.&lt;anonymous&gt; (app/test/test.js:35:26)
  </code></pre>
  
  <p><strong>However when I change the for loop to</strong></p>
  
  <pre><code>for(i = 0; i &lt; 23; i++)
  </code></pre>
  
  <p>All the tests pass except, naturally it doesn't test the last test case.</p>
  
  <pre><code>Time Converter
  main()
    ✓ 12:00:00AM should convert to 00:00:00
    ✓ 01:00:00AM should convert to 01:00:00
    ✓ 02:00:00AM should convert to 02:00:00
    ✓ 03:00:00AM should convert to 03:00:00
    ✓ 04:00:00AM should convert to 04:00:00
    ✓ 05:00:00AM should convert to 05:00:00
    ✓ 06:00:00AM should convert to 06:00:00
    ✓ 07:00:00AM should convert to 07:00:00
    ✓ 08:00:00AM should convert to 08:00:00
    ✓ 09:00:00AM should convert to 09:00:00
    ✓ 10:00:00AM should convert to 10:00:00
    ✓ 11:00:00AM should convert to 11:00:00
    ✓ 12:00:00PM should convert to 12:00:00
    ✓ 01:00:00PM should convert to 13:00:00
    ✓ 02:00:00PM should convert to 14:00:00
    ✓ 03:00:00PM should convert to 15:00:00
    ✓ 04:00:00PM should convert to 16:00:00
    ✓ 05:00:00PM should convert to 17:00:00
    ✓ 06:00:00PM should convert to 18:00:00
    ✓ 07:00:00PM should convert to 19:00:00
    ✓ 08:00:00PM should convert to 20:00:00
    ✓ 09:00:00PM should convert to 21:00:00
    ✓ 10:00:00PM should convert to 22:00:00
    23 passing (14ms)
  </code></pre>
  
  <p>However when I test the last test case by itself, it passes.</p>
  
  <pre><code>✓ 11:00:00PMshould convert to 23:00:00
  </code></pre>
  
  <p><strong>So, I'm confused as to why all the tests don't work if the for loop iterates through the whole array, but works if I iterate up until the last index.  Can someone clear this up for me?</strong>  </p>