Node.js Executing a CronJob while using a mocha for testing

So I have a js code that is a slackbot that should just listen and parse the provided date and then run a CronJob to run a specific function according to the provided cron format or date. Something like that.

var CronJob = require ('cron').CronJob;
...
robot.respond(date, function (msg)) {
if(!isValidDate(date)) msg.reply("not a valid date);
var interval = isCronDate(date) ? date : new Date(date);
msg.reply("Job about to be scheduled.")
var schedule = new CronJob(interval, processDataOnDate(), function() { msg.reply("hello") },  true);
}

      

I have a coffee file checking this code and I expect some answers to come back, but I am NOT expecting a cron job to run based on the date given in my test code. However, this is so. This is normal? Mocha is causing the code to terminate because it is a unit test, or am I doing something wrong? I am running this to execute my unit test.

mocha --compilers coffee:coffee-script/register

      

For more information, I run this as slackbot, so this is all done in the form of "say" and "reply". One of my tests looks like this.

beforeEach ->
    yield @room.user.say 'bob', '@bot schedule at 2017-05-25 18:00:00'
expect(@room.messages).to.eql [
    ['bob', 'bot schedule at 2017-05-25 18:00:00']
    ['bot', 'Job about to be scheduled']

]

      

The test fails and tells me that the actual result included a "hello" message from the bot, even though the date I provided in my test would be in the future.

+7


source to share


1 answer


The last parameter, when you initialize your CronJob, indicates that it should execute the job immediately:

new CronJob(interval, processDataOnDate(), function() { msg.reply("hello") },  true);

      



This will lead to immediate execution of the task, even if its due date is in the future.

See: https://www.npmjs.com/package/cron#api

0


source







All Articles