2

I have a following simple function:

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') > 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;
}

So, if I call it, splitIntoDays('2014/09/13-10:00:00','2014/09/12-09:00:00'), I get the following response:

Error: From date (2014/09/13-10:00:00) is after To date (2014/09/12-09:00:00).Enter a valid date range.

I wrote the following test using Mocha and Chai:

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.'));
        });
    });
});

But, this one fails. Can you please help me in pointing out a mistake?

I tried tried the following as well:

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);
        });
    });
});

And I am getting the following:

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
Prachi g
  • 849
  • 3
  • 9
  • 23
  • possible duplicate of [Mocha / Chai expect.to.throw not catching thrown errors](http://stackoverflow.com/questions/21587122/mocha-chai-expect-to-throw-not-catching-thrown-errors) – Louis Jan 20 '15 at 11:26

1 Answers1

2

You can find an answer here: Mocha / Chai expect.to.throw not catching thrown errors.

Basically, you have to pass a function to expect(). Now you are passing it the result.

EDIT: Pasting the example from my comment which worked.

var expect = require('chai').expect;

describe('test', function(){

    var utils = {
        splitIntoDays : function(from, to){
            throw new Error('Invalid date range.');
        }
    }

    it('throws errors', function(){
        expect(utils.splitIntoDays.bind(utils, '2014/09/13-10:00:00', '2014/09/12-09:00:00')).to.throw(Error, 'Invalid date range.');
    });

});
Community
  • 1
  • 1
juunas
  • 54,244
  • 13
  • 113
  • 149
  • If you check the last part of question, I have tried that as well. But this doesn't help. – Prachi g Jan 20 '15 at 09:31
  • Did you try `expect(utils.splitIntoDays.bind(utils, '2014/09/13-10:00:00','2014/09/12-09:00:00')).throw(Error)` ? (You can also add the error message check) – juunas Jan 20 '15 at 09:35
  • Also could you try just switching `new Error(...)` to `Error`? – juunas Jan 20 '15 at 09:38
  • I am getting the following error after trying the above: `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` – Prachi g Jan 20 '15 at 09:45
  • 2
    The following works for me: `var expect = require('chai').expect; describe.only('test', function(){ var utils = { splitIntoDays : function(from, to){ throw new Error('Invalid date range.'); } } it('throws errors', function(){ expect(utils.splitIntoDays.bind(utils, '2014/09/13-10:00:00', '2014/09/12-09:00:00')).to.throw(Error, 'Invalid date range.'); }); });` – juunas Jan 20 '15 at 09:51