So, I've got some js code which is a slackbot which supposed to simply listen and parse the date provided, then start a CronJob to run a certain function according to the cron or date format provided. Something like this.
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've got a coffee file testing this code, and I expect certain responses back, but I do NOT expect the cron job to be executed based on the date I've provided in my test code. However, it is. Is this normal? Does mocha force the code to finish execution due to the fact that this 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 further information I am running this as a 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 informs me that the actual result included the message 'hello' from the bot, despite the fact that the date I've provided in my test is in the future.