title: Writing test cases using Mocha and Chai for function with Error
body: <p>I have a following simple function:</p>
  
  <pre><code>var moment = require('moment-timezone');
  
  exports.splitIntoDays = function(from,to) {
      var timeIntervals = [];
      var interval = {};
      var start = moment(from);
      var end = moment(to);
      if(start.isAfter(end)) {
          throw new Error('From date ('+from+') is after To date ('+to+').Enter a valid date range.');
      }
      var initial = start;
      console.log("Before loop"+initial.format("YYYY/MM/DD-HH:mm:ss")+"  "+initial.diff(end,'hours'));
      while(end.diff(initial,'hours') &gt; 24) {
          timeIntervals.push({"from" : initial.format("YYYY/MM/DD-HH:mm:ss"), "to" : initial.add(24,'hours').format("YYYY/MM/DD-HH:mm:ss")});
          initial = initial.add(1,'hours');
      }
      timeIntervals.push({"from" : initial.format("YYYY/MM/DD-HH:mm:ss"), "to" : end.format("YYYY/MM/DD-HH:mm:ss")});
      console.info(JSON.stringify(timeIntervals));
      return timeIntervals;
  }
  </code></pre>
  
  <p>So, if I call it, <code>splitIntoDays('2014/09/13-10:00:00','2014/09/12-09:00:00')</code>, I get the following response:</p>
  
  <pre><code>Error: From date (2014/09/13-10:00:00) is after To date (2014/09/12-09:00:00).Enter a valid date range.
  </code></pre>
  
  <p>I wrote the following test using Mocha and Chai:</p>
  
  <pre><code>var expect = require("chai").expect;
  var utils = require("../Utils.js");
  
  describe("Utils", function(){
      describe("#splitIntoDays()", function(){
          it("equal", function () {
              var results = utils.splitIntoDays('2014/09/13-10:00:00','2014/09/12-09:00:00');
              expect(utils.splitIntoDays('2014/09/13-10:00:00','2014/09/12-09:00:00')).to.throw(new Error('From date (2014/09/13-10:00:00) is after To date (2014/09/12-09:00:00).Enter a valid date range.'));
          });
      });
  });
  </code></pre>
  
  <p>But, this one fails. Can you please help me in pointing out a mistake?</p>
  
  <p>I tried tried the following as well:</p>
  
  <pre><code>describe("Utils", function(){
      describe("#splitIntoDays()", function(){
          var error = new Error('From date (2014/09/13-10:00:00) is after To date (2014/09/12-09:00:00).Enter a valid date range.');
          it("equal", function () {
              expect(function(){
                  utils.splitIntoDays('2014/09/13-10:00:00','2014/09/12-09:00:00');
              }).to.throw(error);
          });
      });
  });
  </code></pre>
  
  <p>And I am getting the following:</p>
  
  <pre><code>AssertionError: expected [Function] to throw 'Error: From date (2014/09/13-10:00:00) is after To date (2014/09/12-09:00:00).Enter a valid date range.' but 'Error: From date (2014/09/13-10:00:00) is after To date (2014/09/12-09:00:00).Enter a valid date range.' was thrown
  </code></pre>