Repository: kelektiv/node-cron
Pull request #519 (2020-08-14 19:15:37)

fix!: return empty array from nextDates when called without argument
## Proposed changes

- Make sure that `nextDates` return an array(as described in the README.md) even if it's called without an argument(in this case an empty array)
- Add `nextDate` description in the README.md

## Current behaviour

- `nextDates` return a moment object, if it's called without an argument

## Details

- This change could break someone's code!!
- If this PR is not accepted, I will make another PR just to update the description of the behaviour of `nextDates` function in the READM.md


codecov[bot]: # [Codecov](https://codecov.io/gh/kelektiv/node-cron/pull/519?src=pr&el=h1) Report
> Merging [#519](https://codecov.io/gh/kelektiv/node-cron/pull/519?src=pr&el=desc) into [master](https://codecov.io/gh/kelektiv/node-cron/commit/9a5172661be362af2871b1c013f126775fbab808&el=desc) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/kelektiv/node-cron/pull/519/graphs/tree.svg?width=650&height=150&src=pr&token=NEb2k5YQsZ)](https://codecov.io/gh/kelektiv/node-cron/pull/519?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #519   +/-   ##
=======================================
  Coverage   92.35%   92.35%           
=======================================
  Files           3        3           
  Lines         353      353           
  Branches      102      103    +1     
=======================================
  Hits          326      326           
  Misses         26       26           
  Partials        1        1           
```


| [Impacted Files](https://codecov.io/gh/kelektiv/node-cron/pull/519?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [lib/job.js](https://codecov.io/gh/kelektiv/node-cron/pull/519/diff?src=pr&el=tree#diff-bGliL2pvYi5qcw==) | `90.99% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kelektiv/node-cron/pull/519?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kelektiv/node-cron/pull/519?src=pr&el=footer). Last update [9a51726...4d2932f](https://codecov.io/gh/kelektiv/node-cron/pull/519?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index 28d4aa9..6ad7d62 100644
--- a/README.md
+++ b/README.md
@@ -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,
@@ -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.
diff --git a/lib/job.js b/lib/job.js
index a3b12b5..dbbce8d 100644
--- a/lib/job.js
+++ b/lib/job.js
@@ -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 () {


Test patch:
diff --git a/tests/cron.test.js b/tests/cron.test.js
index 601143d..69576fd 100644
--- a/tests/cron.test.js
+++ b/tests/cron.test.js
@@ -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', () => {