Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: return empty array from nextDates when called without argument #519

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -963,32 +963,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