cron 1.8.0 → 1.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable no-new */
2
2
  const sinon = require('sinon');
3
3
  const cron = require('../lib/cron');
4
+
4
5
  describe('cron', () => {
5
6
  let clock;
6
7
 
@@ -26,7 +27,7 @@ describe('cron', () => {
26
27
  const job = new cron.CronJob(
27
28
  '* * * * * *',
28
29
  callback,
29
- function() {
30
+ () => {
30
31
  expect(callback).toHaveBeenCalledTimes(1);
31
32
  done();
32
33
  },
@@ -66,7 +67,7 @@ describe('cron', () => {
66
67
  const job = new cron.CronJob(
67
68
  '* * * * * *',
68
69
  callback,
69
- function() {
70
+ () => {
70
71
  expect(callback).toHaveBeenCalledTimes(5);
71
72
  done();
72
73
  },
@@ -109,7 +110,7 @@ describe('cron', () => {
109
110
  const job = new cron.CronJob(
110
111
  '*/1 * * * * *',
111
112
  callback,
112
- function() {
113
+ () => {
113
114
  expect(callback).toHaveBeenCalledTimes(5);
114
115
  done();
115
116
  },
@@ -129,12 +130,12 @@ describe('cron', () => {
129
130
  expect(callback).toHaveBeenCalledTimes(8);
130
131
  });
131
132
 
132
- it('should run every second for a range ([start]-[end] * * * * *)', done => {
133
+ it('should run every second for a range ([start]-[end] * * * * *) with onComplete', done => {
133
134
  const callback = jest.fn();
134
135
  const job = new cron.CronJob(
135
136
  '0-8 * * * * *',
136
137
  callback,
137
- function() {
138
+ () => {
138
139
  expect(callback).toHaveBeenCalledTimes(8);
139
140
  done();
140
141
  },
@@ -179,7 +180,7 @@ describe('cron', () => {
179
180
  const job = new cron.CronJob({
180
181
  cronTime: '* * * * * *',
181
182
  onTick: callback,
182
- onComplete: function() {
183
+ onComplete: () => {
183
184
  expect(callback).toHaveBeenCalledTimes(1);
184
185
  done();
185
186
  },
@@ -197,7 +198,7 @@ describe('cron', () => {
197
198
  const l = [];
198
199
  const job = new cron.CronJob(
199
200
  '00 30 * * * *',
200
- function() {
201
+ () => {
201
202
  l.push(Math.floor(Date.now() / 60000));
202
203
  },
203
204
  null,
@@ -206,7 +207,7 @@ describe('cron', () => {
206
207
 
207
208
  clock.tick(m60 * 10);
208
209
 
209
- expect(l.length).toBe(10);
210
+ expect(l).toHaveLength(10);
210
211
  expect(l.every(i => i % 30 === 0)).toBe(true);
211
212
 
212
213
  job.stop();
@@ -222,12 +223,12 @@ describe('cron', () => {
222
223
  expect(callback).toHaveBeenCalledTimes(4);
223
224
  });
224
225
 
225
- it('should run every 45 minutes for 2 hours (0 */45 * * * *)', done => {
226
+ it('should run every 45 minutes for 2 hours (0 */45 * * * *) with onComplete', done => {
226
227
  const callback = jest.fn();
227
228
  const job = new cron.CronJob(
228
229
  '0 */45 * * * *',
229
230
  callback,
230
- function() {
231
+ () => {
231
232
  expect(callback).toHaveBeenCalledTimes(4);
232
233
  done();
233
234
  },
@@ -239,15 +240,14 @@ describe('cron', () => {
239
240
  });
240
241
  });
241
242
 
242
- it('should start and stop job', done => {
243
+ it('should start and stop job from outside', done => {
243
244
  const callback = jest.fn();
244
245
  const job = new cron.CronJob(
245
246
  '* * * * * *',
246
- function() {
247
+ function () {
247
248
  callback();
248
- this.stop();
249
249
  },
250
- function() {
250
+ () => {
251
251
  expect(callback).toHaveBeenCalledTimes(1);
252
252
  clock.restore();
253
253
  done();
@@ -258,6 +258,24 @@ describe('cron', () => {
258
258
  job.stop();
259
259
  });
260
260
 
261
+ it('should start and stop job from inside (default context)', done => {
262
+ const callback = jest.fn();
263
+ new cron.CronJob(
264
+ '* * * * * *',
265
+ function () {
266
+ callback();
267
+ this.stop();
268
+ },
269
+ () => {
270
+ expect(callback).toHaveBeenCalledTimes(1);
271
+ clock.restore();
272
+ done();
273
+ },
274
+ true
275
+ );
276
+ clock.tick(1000);
277
+ });
278
+
261
279
  describe('with date', () => {
262
280
  it('should run on a specific date', () => {
263
281
  const d = new Date();
@@ -267,7 +285,7 @@ describe('cron', () => {
267
285
  const callback = jest.fn();
268
286
  const job = new cron.CronJob(
269
287
  d,
270
- function() {
288
+ () => {
271
289
  var t = new Date();
272
290
  expect(t.getSeconds()).toBe(d.getSeconds());
273
291
  callback();
@@ -289,12 +307,12 @@ describe('cron', () => {
289
307
  const callback = jest.fn();
290
308
  const job = new cron.CronJob(
291
309
  d,
292
- function() {
310
+ () => {
293
311
  var t = new Date();
294
312
  expect(t.getSeconds()).toBe(d.getSeconds());
295
313
  callback();
296
314
  },
297
- function() {
315
+ () => {
298
316
  expect(callback).toHaveBeenCalledTimes(1);
299
317
  done();
300
318
  },
@@ -305,7 +323,7 @@ describe('cron', () => {
305
323
  job.stop();
306
324
  });
307
325
 
308
- it('should wait and not fire immediately', function() {
326
+ it('should wait and not fire immediately', () => {
309
327
  const clock = sinon.useFakeTimers();
310
328
  const callback = jest.fn();
311
329
 
@@ -320,37 +338,57 @@ describe('cron', () => {
320
338
  job.stop();
321
339
  expect(callback).toHaveBeenCalledTimes(0);
322
340
  });
341
+
342
+ it('should wait but fire on init', () => {
343
+ const clock = sinon.useFakeTimers();
344
+ const callback = jest.fn();
345
+
346
+ const d = new Date().getTime() + 31 * 86400 * 1000;
347
+
348
+ var job = cron.job({
349
+ cronTime: new Date(d),
350
+ onTick: callback,
351
+ runOnInit: true
352
+ });
353
+
354
+ expect(callback).toHaveBeenCalledTimes(1);
355
+
356
+ job.start();
357
+
358
+ clock.tick(1000);
359
+
360
+ clock.restore();
361
+ job.stop();
362
+ expect(callback).toHaveBeenCalledTimes(1);
363
+ });
323
364
  });
324
365
 
325
366
  describe('with timezone', () => {
326
- it('should run a job using cron syntax', function() {
367
+ it('should run a job using cron syntax', () => {
327
368
  const callback = jest.fn();
328
- const moment = require('moment-timezone');
369
+ const luxon = require('luxon');
329
370
  let zone = 'America/Chicago';
330
-
331
371
  // New Orleans time
332
- const t = moment();
333
- t.tz(zone);
334
-
372
+ let t = luxon.DateTime.local().setZone(zone);
335
373
  // Current time
336
- const d = moment();
374
+ const d = luxon.DateTime.local();
337
375
 
338
376
  // If current time is New Orleans time, switch to Los Angeles..
339
- if (t.hours() === d.hours()) {
377
+ if (t.hour === d.hour) {
340
378
  zone = 'America/Los_Angeles';
341
- t.tz(zone);
379
+ t = t.setZone(zone);
342
380
  }
343
- expect(d.hours()).not.toBe(t.hours());
381
+ expect(d.hour).not.toBe(t.hour);
344
382
 
345
383
  // If t = 59s12m then t.setSeconds(60)
346
384
  // becomes 00s13m so we're fine just doing
347
385
  // this and no testRun callback.
348
- t.add(1, 's');
386
+ t = t.plus({ seconds: 1 });
349
387
  // Run a job designed to be executed at a given
350
388
  // time in `zone`, making sure that it is a different
351
389
  // hour than local time.
352
390
  const job = new cron.CronJob(
353
- t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *',
391
+ t.second + ' ' + t.minute + ' ' + t.hour + ' * * *',
354
392
  callback,
355
393
  null,
356
394
  true,
@@ -363,45 +401,44 @@ describe('cron', () => {
363
401
  expect(callback).toHaveBeenCalledTimes(1);
364
402
  });
365
403
 
366
- it('should run a job using a date', function() {
367
- const moment = require('moment-timezone');
404
+ it('should run a job using a date', () => {
405
+ const luxon = require('luxon');
368
406
  let zone = 'America/Chicago';
369
407
  // New Orleans time
370
- const t = moment();
371
- t.tz(zone);
408
+ let t = luxon.DateTime.local().setZone(zone);
372
409
  // Current time
373
- const d = moment();
410
+ let d = luxon.DateTime.local();
374
411
 
375
412
  // If current time is New Orleans time, switch to Los Angeles..
376
- if (t.hours() === d.hours()) {
413
+ if (t.hour === d.hour) {
377
414
  zone = 'America/Los_Angeles';
378
- t.tz(zone);
415
+ t = t.setZone(zone);
379
416
  }
380
417
 
381
- expect(d.hours()).not.toBe(t.hours());
382
- d.add(1, 'second');
418
+ expect(d.hour).not.toBe(t.hour);
419
+ d = d.plus({ seconds: 1 });
383
420
  const clock = sinon.useFakeTimers(d.valueOf());
384
421
  const callback = jest.fn();
385
- const job = new cron.CronJob(d._d, callback, null, true, zone);
422
+ const job = new cron.CronJob(d.toJSDate(), callback, null, true, zone);
386
423
  clock.tick(1000);
387
424
  clock.restore();
388
425
  job.stop();
389
426
  expect(callback).toHaveBeenCalledTimes(1);
390
427
  });
391
428
 
392
- it('should test if timezone is valid.', function() {
393
- expect(function() {
429
+ it('should test if timezone is valid.', () => {
430
+ expect(() => {
394
431
  // eslint-disable-next-line no-new
395
432
  new cron.CronJob({
396
433
  cronTime: '* * * * * *',
397
- onTick: function() {},
434
+ onTick: () => {},
398
435
  timeZone: 'fake/timezone'
399
436
  });
400
437
  }).toThrow();
401
438
  });
402
439
  });
403
440
 
404
- it('should start, change time, start again', function() {
441
+ it('should start, change time, start again', () => {
405
442
  const callback = jest.fn();
406
443
  const clock = sinon.useFakeTimers();
407
444
 
@@ -422,7 +459,15 @@ describe('cron', () => {
422
459
  expect(callback).toHaveBeenCalledTimes(3);
423
460
  });
424
461
 
425
- it('should start, change time, exception', function() {
462
+ it('should setTime with invalid object', () => {
463
+ const callback = jest.fn();
464
+ const job = new cron.CronJob('* * * * * *', callback);
465
+ expect(() => {
466
+ job.setTime(undefined);
467
+ }).toThrow();
468
+ });
469
+
470
+ it('should start, change time, exception', () => {
426
471
  const callback = jest.fn();
427
472
  var clock = sinon.useFakeTimers();
428
473
 
@@ -434,7 +479,7 @@ describe('cron', () => {
434
479
  clock.tick(1000);
435
480
 
436
481
  job.stop();
437
- expect(function() {
482
+ expect(() => {
438
483
  job.setTime(time);
439
484
  }).toThrow();
440
485
 
@@ -443,12 +488,12 @@ describe('cron', () => {
443
488
  expect(callback).toHaveBeenCalledTimes(1);
444
489
  });
445
490
 
446
- it('should scope onTick to running job', function() {
491
+ it('should scope onTick to running job', () => {
447
492
  const clock = sinon.useFakeTimers();
448
493
 
449
494
  const job = new cron.CronJob(
450
495
  '* * * * * *',
451
- function() {
496
+ function () {
452
497
  expect(job).toBeInstanceOf(cron.CronJob);
453
498
  expect(job).toEqual(this);
454
499
  },
@@ -462,12 +507,12 @@ describe('cron', () => {
462
507
  job.stop();
463
508
  });
464
509
 
465
- it('should scope onTick to object', function() {
510
+ it('should scope onTick to object', () => {
466
511
  const clock = sinon.useFakeTimers();
467
512
 
468
513
  const job = new cron.CronJob(
469
514
  '* * * * * *',
470
- function() {
515
+ function () {
471
516
  expect(this.hello).toEqual('world');
472
517
  expect(job).not.toEqual(this);
473
518
  },
@@ -483,12 +528,12 @@ describe('cron', () => {
483
528
  job.stop();
484
529
  });
485
530
 
486
- it('should scope onTick to object within contstructor object', function() {
531
+ it('should scope onTick to object within constructor object', () => {
487
532
  const clock = sinon.useFakeTimers();
488
533
 
489
534
  const job = new cron.CronJob({
490
535
  cronTime: '* * * * * *',
491
- onTick: function() {
536
+ onTick: function () {
492
537
  expect(this.hello).toEqual('world');
493
538
  expect(job).not.toEqual(this);
494
539
  },
@@ -502,11 +547,11 @@ describe('cron', () => {
502
547
  job.stop();
503
548
  });
504
549
 
505
- it('should not get into an infinite loop on invalid times', function() {
506
- expect(function() {
550
+ it('should not get into an infinite loop on invalid times', () => {
551
+ expect(() => {
507
552
  new cron.CronJob(
508
553
  '* 60 * * * *',
509
- function() {
554
+ () => {
510
555
  expect.ok(true);
511
556
  },
512
557
  null,
@@ -514,10 +559,10 @@ describe('cron', () => {
514
559
  );
515
560
  }).toThrow();
516
561
 
517
- expect(function() {
562
+ expect(() => {
518
563
  new cron.CronJob(
519
564
  '* * 24 * * *',
520
- function() {
565
+ () => {
521
566
  expect.ok(true);
522
567
  },
523
568
  null,
@@ -526,7 +571,7 @@ describe('cron', () => {
526
571
  }).toThrow();
527
572
  });
528
573
 
529
- it('should test start of month', function() {
574
+ it('should test start of month', () => {
530
575
  const callback = jest.fn();
531
576
  const d = new Date('12/31/2014');
532
577
  d.setSeconds(59);
@@ -549,7 +594,7 @@ describe('cron', () => {
549
594
  expect(callback).toHaveBeenCalledTimes(3);
550
595
  });
551
596
 
552
- it('should not fire if time was adjusted back', function() {
597
+ it('should not fire if time was adjusted back', () => {
553
598
  const callback = jest.fn();
554
599
  const clock = sinon.useFakeTimers({
555
600
  toFake: ['setTimeout']
@@ -564,7 +609,7 @@ describe('cron', () => {
564
609
  job.stop();
565
610
  });
566
611
 
567
- it('should run every day', function() {
612
+ it('should run every day', () => {
568
613
  const callback = jest.fn();
569
614
  const d = new Date('12/31/2014');
570
615
  d.setSeconds(59);
@@ -587,7 +632,7 @@ describe('cron', () => {
587
632
  expect(callback).toHaveBeenCalledTimes(14);
588
633
  });
589
634
 
590
- it('should run every 2 hours between hours', function() {
635
+ it('should run every 2 hours between hours', () => {
591
636
  const callback = jest.fn();
592
637
  const d = new Date('12/31/2014');
593
638
  d.setSeconds(0);
@@ -614,7 +659,7 @@ describe('cron', () => {
614
659
  expect(callback).toHaveBeenCalledTimes(3);
615
660
  });
616
661
 
617
- it('should run every minute', function() {
662
+ it('should run every minute', () => {
618
663
  const callback = jest.fn();
619
664
  const d = new Date('12/31/2014');
620
665
  d.setSeconds(0);
@@ -638,7 +683,7 @@ describe('cron', () => {
638
683
  expect(callback).toHaveBeenCalledTimes(2);
639
684
  });
640
685
 
641
- it('should run every day', function() {
686
+ it('should run every day at 12:30', () => {
642
687
  const callback = jest.fn();
643
688
  const d = new Date('12/31/2014');
644
689
  d.setSeconds(0);
@@ -666,7 +711,7 @@ describe('cron', () => {
666
711
  expect(callback).toHaveBeenCalledTimes(8);
667
712
  });
668
713
 
669
- it('should trigger onTick at midnight', function() {
714
+ it('should trigger onTick at midnight', () => {
670
715
  const callback = jest.fn();
671
716
  const d = new Date('12/31/2014');
672
717
  d.setSeconds(59);
@@ -689,7 +734,7 @@ describe('cron', () => {
689
734
  expect(callback).toHaveBeenCalledTimes(1);
690
735
  });
691
736
 
692
- it('should run every day UTC', function() {
737
+ it('should run every day UTC', () => {
693
738
  const callback = jest.fn();
694
739
  const d = new Date('12/31/2014');
695
740
  d.setSeconds(0);
@@ -719,7 +764,7 @@ describe('cron', () => {
719
764
  });
720
765
 
721
766
  // from https://github.com/kelektiv/node-cron/issues/180#issuecomment-154108131
722
- it('should run once not double', function() {
767
+ it('should run once not double', () => {
723
768
  const callback = jest.fn();
724
769
  const d = new Date(2015, 1, 1, 1, 1, 41, 0);
725
770
  const clock = sinon.useFakeTimers(d.getTime());
@@ -738,21 +783,18 @@ describe('cron', () => {
738
783
  expect(callback).toHaveBeenCalledTimes(1);
739
784
  });
740
785
 
741
- describe('with utcOffset', function() {
742
- it('should run a job using cron syntax with number format utcOffset', function() {
786
+ describe('with utcOffset', () => {
787
+ it('should run a job using cron syntax with number format utcOffset', () => {
743
788
  const clock = sinon.useFakeTimers();
744
789
  const callback = jest.fn();
745
-
746
- const moment = require('moment-timezone');
747
-
790
+ const luxon = require('luxon');
748
791
  // Current time
749
- const t = moment();
750
-
792
+ const t = luxon.DateTime.local();
751
793
  // UTC Offset decreased by an hour
752
- const utcOffset = t.utcOffset() - 60;
794
+ const utcOffset = t.offset - 60;
753
795
 
754
796
  const job = new cron.CronJob(
755
- t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *',
797
+ t.second + ' ' + t.minute + ' ' + t.hour + ' * * *',
756
798
  callback,
757
799
  null,
758
800
  true,
@@ -772,24 +814,21 @@ describe('cron', () => {
772
814
  expect(callback).toHaveBeenCalledTimes(1);
773
815
  });
774
816
 
775
- it('should run a job using cron syntax with string format utcOffset', function() {
817
+ it('should run a job using cron syntax with string format utcOffset', () => {
776
818
  const clock = sinon.useFakeTimers();
777
819
  const callback = jest.fn();
778
-
779
- const moment = require('moment-timezone');
780
-
820
+ const luxon = require('luxon');
781
821
  // Current time
782
- const t = moment();
783
-
822
+ const t = luxon.DateTime.local();
784
823
  // UTC Offset decreased by an hour (string format '(+/-)HH:mm')
785
- const utcOffset = t.utcOffset() - 60;
824
+ const utcOffset = t.offset - 60;
786
825
  let utcOffsetString = utcOffset > 0 ? '+' : '-';
787
826
  utcOffsetString += ('0' + Math.floor(Math.abs(utcOffset) / 60)).slice(-2);
788
827
  utcOffsetString += ':';
789
828
  utcOffsetString += ('0' + (utcOffset % 60)).slice(-2);
790
829
 
791
830
  var job = new cron.CronJob(
792
- t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *',
831
+ t.second + ' ' + t.minute + ' ' + t.hour + ' * * *',
793
832
  callback,
794
833
  null,
795
834
  true,
@@ -810,7 +849,7 @@ describe('cron', () => {
810
849
  expect(callback).toHaveBeenCalledTimes(1);
811
850
  });
812
851
 
813
- it('should run a job using cron syntax with number format utcOffset that is 0', function() {
852
+ it('should run a job using cron syntax with number format utcOffset that is 0', () => {
814
853
  const clock = sinon.useFakeTimers();
815
854
  const callback = jest.fn();
816
855
 
@@ -834,10 +873,65 @@ describe('cron', () => {
834
873
  expect(callback).toHaveBeenCalledTimes(1);
835
874
  });
836
875
 
837
- it('should be able to detect out of range days of month', function() {
838
- expect(function() {
876
+ it('should be able to detect out of range days of month', () => {
877
+ expect(() => {
839
878
  new cron.CronTime('* * 32 FEB *');
840
879
  }).toThrow();
841
880
  });
842
881
  });
882
+
883
+ it('should give the next date to run at', () => {
884
+ const callback = jest.fn();
885
+ const clock = sinon.useFakeTimers();
886
+ const job = new cron.CronJob('* * * * * *', callback);
887
+ const d = Date.now();
888
+
889
+ expect(job.nextDate().toMillis()).toEqual(d + 1000);
890
+
891
+ clock.restore();
892
+ });
893
+
894
+ it('should give the next dates to run at', () => {
895
+ const callback = jest.fn();
896
+ const clock = sinon.useFakeTimers();
897
+ const job = new cron.CronJob('* * * * * *', callback);
898
+ const d = Date.now();
899
+
900
+ expect(job.nextDates(5).map(d => d.toMillis())).toEqual([
901
+ d + 1000,
902
+ d + 2000,
903
+ d + 3000,
904
+ d + 4000,
905
+ d + 5000
906
+ ]);
907
+
908
+ clock.restore();
909
+ });
910
+
911
+ it('should automatically setup a new timeout if we roll past the max timeout delay', () => {
912
+ const callback = jest.fn();
913
+ const clock = sinon.useFakeTimers();
914
+ const d = new Date();
915
+ d.setMilliseconds(2147485647 * 2); // MAXDELAY in `job.js` + 2000.
916
+ const job = new cron.CronJob(d, callback);
917
+ job.start();
918
+ clock.tick(2147483648);
919
+ expect(callback).toHaveBeenCalledTimes(0);
920
+ clock.tick(2147489648);
921
+ expect(callback).toHaveBeenCalledTimes(1);
922
+ job.stop();
923
+ clock.restore();
924
+ });
925
+
926
+ it('should give the last execution date', () => {
927
+ const callback = jest.fn();
928
+ const clock = sinon.useFakeTimers();
929
+ const job = new cron.CronJob('* * * * * *', callback);
930
+ job.start();
931
+ clock.tick(1000);
932
+ expect(callback).toHaveBeenCalledTimes(1);
933
+ expect(job.lastDate().getTime()).toEqual(1000);
934
+ job.stop();
935
+ clock.restore();
936
+ });
843
937
  });