From 141aa00f55fa105d89df7e257d82c94ad2bb2b3a Mon Sep 17 00:00:00 2001 From: Pierre Cavin Date: Fri, 13 Oct 2023 00:34:55 +0200 Subject: [PATCH] fix: fix lastDate() value for intervals > 25 days (#711) --- src/job.ts | 2 +- tests/cron.test.ts | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/job.ts b/src/job.ts index 26c732e9..ca6f6f14 100644 --- a/src/job.ts +++ b/src/job.ts @@ -241,7 +241,6 @@ export class CronJob { // again. This processing might make us miss the deadline by a few ms // times the number of sleep sessions. Given a MAXDELAY of almost a // month, this should be no issue. - this.lastExecution = new Date(); if (remaining) { if (remaining > MAXDELAY) { remaining -= MAXDELAY; @@ -254,6 +253,7 @@ export class CronJob { setCronTimeout(timeout); } else { // We have arrived at the correct point in time. + this.lastExecution = new Date(); this.running = false; diff --git a/tests/cron.test.ts b/tests/cron.test.ts index f603cd00..0ef6a883 100644 --- a/tests/cron.test.ts +++ b/tests/cron.test.ts @@ -1137,7 +1137,7 @@ describe('cron', () => { clock.restore(); }); - it('should give the last execution date', () => { + it('should give the correct last execution date', () => { const callback = jest.fn(); const clock = sinon.useFakeTimers(); const job = new CronJob('* * * * * *', callback); @@ -1149,6 +1149,23 @@ describe('cron', () => { clock.restore(); }); + it('should give the correct last execution date for intervals greater than 25 days (#710)', () => { + const callback = jest.fn(); + const clock = sinon.useFakeTimers(); + + const job = new CronJob('0 0 0 1 * *', callback); // At 00:00 on day-of-month 1. + job.start(); + + // tick one tick before nextDate() + clock.tick(job.nextDate().toMillis() - 1); + + expect(callback).toHaveBeenCalledTimes(0); + expect(job.lastDate()?.getTime()).toBeUndefined(); + + job.stop(); + clock.restore(); + }); + it('should throw when providing both exclusive parameters timeZone and utcOffset', () => { expect(() => { // @ts-expect-error testing runtime exception