From 61d26e145960a7f1ac15dea7525f42fff231c43d Mon Sep 17 00:00:00 2001 From: Subhi Al Hasan Date: Tue, 14 Aug 2018 01:51:10 +0300 Subject: [PATCH] adding tests and fixing a bug --- lib/cron.js | 3 ++- tests/test-crontime.js | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/cron.js b/lib/cron.js index 4c007d9c..5de922c1 100644 --- a/lib/cron.js +++ b/lib/cron.js @@ -176,6 +176,7 @@ _getNextDateFrom: function(start,zone) { var date; + var firstDate=moment(start).valueOf(); if(zone){ date=moment(start).tz(zone); } @@ -262,7 +263,7 @@ continue; } - if(date.valueOf()==start){ + if(date.valueOf() === firstDate){ date.seconds(date.seconds()+1); continue; } diff --git a/tests/test-crontime.js b/tests/test-crontime.js index 38b8f5bf..fdd3704e 100644 --- a/tests/test-crontime.js +++ b/tests/test-crontime.js @@ -202,4 +202,26 @@ describe('crontime', function () { var x = cronTime._getNextDateFrom(new Date("2018-08-10T02:19:59.999Z")); expect(x.toISOString()).to.equal('2018-08-10T02:20:00.000Z'); }); + + it('should generete the right next days when cron is set to every minute', function () { + var cronTime = new cron.CronTime('* * * * *'); + var min=60000; + var previousDate=new Date(Date.UTC(2018,5,3,0,0)); + for(var i=0;i<25;i++){ + var nextDate = cronTime._getNextDateFrom(previousDate); + expect(nextDate.valueOf()).to.equal(previousDate.valueOf()+min) + previousDate=nextDate; + } + }); + + it('should generete the right next days when cron is set to every 15 min', function () { + var cronTime = new cron.CronTime('*/15 * * * *'); + var min=60000*15; + var previousDate=new Date(Date.UTC(2016,6,3,0,0)); + for(var i=0;i<25;i++){ + var nextDate = cronTime._getNextDateFrom(previousDate); + expect(nextDate.valueOf()).to.equal(previousDate.valueOf()+min) + previousDate=nextDate; + } + }); });