Skip to content

Commit

Permalink
test: added test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Aug 14, 2023
1 parent a5edee7 commit 5dbc4ee
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tests/cron.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ describe('cron', () => {
const clock = sinon.useFakeTimers();
const callback = jest.fn();

var job = cron.job({
const job = cron.job({
cronTime: '* * * * * *',
onTick: callback,
runOnInit: true
Expand Down Expand Up @@ -768,6 +768,35 @@ describe('cron', () => {
expect(callback).toHaveBeenCalledTimes(1);
});

/**
* maximum match interval is 8 years:
* crontab has '* * 29 2 *' and we are on 1 March 2096:
* next matching time will be 29 February 2104
* source: https://github.com/cronie-crond/cronie/blob/0d669551680f733a4bdd6bab082a0b3d6d7f089c/src/cronnext.c#L401-L403
*/
it('should work correctly for max match interval', () => {
const callback = jest.fn();
const d = new Date(2096, 2, 1);
const clock = sinon.useFakeTimers(d.getTime());

const job = new cron.CronJob({
cronTime: ' * * 29 1 *',
onTick: callback,
start: true
});

// 7 years, 11 months and 27 days
const almostEightYears = 2919 * 24 * 60 * 60 * 1000;
clock.tick(almostEightYears);
expect(callback).toHaveBeenCalledTimes(0);

// tick by 1 day
clock.tick(24 * 60 * 60 * 1000);
clock.restore();
job.stop();
expect(callback).toHaveBeenCalledTimes(1);
});

describe('with utcOffset', () => {
it('should run a job using cron syntax with number format utcOffset', () => {
const clock = sinon.useFakeTimers();
Expand Down

0 comments on commit 5dbc4ee

Please sign in to comment.