Skip to content

Commit

Permalink
fix!: return empty array from nextDates when called without argument (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
oxidia authored and sheerlox committed Sep 30, 2023
1 parent 4a406c1 commit c2891ba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ As goes with semver, breaking backwards compatibility should be explicit in the
var CronJob = require('cron').CronJob;
var job = new CronJob(
'* * * * * *',
function() {
function () {
console.log('You will see this message every second');
},
null,
Expand Down Expand Up @@ -125,6 +125,7 @@ Parameter Based
- `stop` - Stops your job.
- `setTime` - Stops and changes the time for the `CronJob`. Param must be a `CronTime`.
- `lastDate` - Tells you the last execution date.
- `nextDate` - Provides the next date that will trigger an `onTick`.
- `nextDates` - Provides an array of the next set of dates that will trigger an `onTick`.
- `fireOnTick` - Allows you to override the `onTick` calling behavior. This matters so only do this if you have a really good reason to do so.
- `addCallback` - Allows you to add `onTick` callbacks.
Expand Down
2 changes: 1 addition & 1 deletion lib/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function CronJob(CronTime, spawn) {
CJ.prototype.fireOnTick = fireOnTick;

CJ.prototype.nextDates = function (i) {
return this.cronTime.sendAt(i);
return this.cronTime.sendAt(i || 0);
};

const start = function () {
Expand Down
54 changes: 33 additions & 21 deletions tests/cron.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,32 +1049,44 @@ describe('cron', () => {
});
});

it('should give the next date to run at', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();
const job = new cron.CronJob('* * * * * *', callback);
const d = Date.now();
describe('nextDate(s)', () => {
it('should give the next date to run at', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();
const job = new cron.CronJob('* * * * * *', callback);
const d = Date.now();

expect(job.nextDate().toMillis()).toEqual(d + 1000);
expect(job.nextDate().toMillis()).toEqual(d + 1000);

clock.restore();
});
clock.restore();
});

it('should give the next dates to run at', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();
const job = new cron.CronJob('* * * * * *', callback);
const d = Date.now();
it('should give the next 5 dates to run at', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();
const job = new cron.CronJob('* * * * * *', callback);
const d = Date.now();

expect(job.nextDates(5).map(d => d.toMillis())).toEqual([
d + 1000,
d + 2000,
d + 3000,
d + 4000,
d + 5000
]);
expect(job.nextDates(5).map(d => d.toMillis())).toEqual([
d + 1000,
d + 2000,
d + 3000,
d + 4000,
d + 5000
]);

clock.restore();
clock.restore();
});

it('should give an empty array when called without argument', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();
const job = new cron.CronJob('* * * * * *', callback);

expect(job.nextDates()).toHaveLength(0);

clock.restore();
});
});

it('should automatically setup a new timeout if we roll past the max timeout delay', () => {
Expand Down

0 comments on commit c2891ba

Please sign in to comment.