Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

node-cron-test.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const { assert } = require('chai');
  2. const sinon = require('sinon');
  3. const cron = require('../src/node-cron');
  4. describe('node-cron', () => {
  5. beforeEach(() => {
  6. this.clock = sinon.useFakeTimers(new Date(2018, 0, 1, 0, 0, 0, 0));
  7. });
  8. afterEach(() => {
  9. this.clock.restore();
  10. });
  11. describe('schedule', () => {
  12. it('should schedule a task', () => {
  13. let executed = 0;
  14. cron.schedule('* * * * * *', () => {
  15. executed += 1;
  16. });
  17. this.clock.tick(2000);
  18. assert.equal(2, executed);
  19. });
  20. it('should schedule a task with America/Sao_Paulo timezone', (done) => {
  21. let startDate = new Date('Thu, 20 Sep 2018 00:00:00.000Z');
  22. this.clock.restore();
  23. this.clock = sinon.useFakeTimers(startDate);
  24. cron.schedule('* * * * * *', (date) => {
  25. assert.equal(19, date.getDate());
  26. assert.equal(8, date.getMonth());
  27. assert.equal(2018, date.getFullYear());
  28. assert.equal(21, date.getHours());
  29. assert.equal(0, date.getMinutes());
  30. assert.equal(1, date.getSeconds());
  31. done();
  32. }, {
  33. timezone: 'America/Sao_Paulo'
  34. });
  35. this.clock.tick(1000);
  36. });
  37. it('should schedule a task with Europe/Rome timezone', (done) => {
  38. let startDate = new Date('Thu, 20 Sep 2018 00:00:00.000Z');
  39. this.clock.restore();
  40. this.clock = sinon.useFakeTimers(startDate);
  41. cron.schedule('* * * * * *', (date) => {
  42. assert.equal(20, date.getDate());
  43. assert.equal(8, date.getMonth());
  44. assert.equal(2018, date.getFullYear());
  45. assert.equal(2, date.getHours());
  46. assert.equal(0, date.getMinutes());
  47. assert.equal(1, date.getSeconds());
  48. done();
  49. }, {
  50. timezone: 'Europe/Rome'
  51. });
  52. this.clock.tick(1000);
  53. });
  54. it('should schedule a task stoped', () => {
  55. let executed = 0;
  56. cron.schedule('* * * * * *', () => {
  57. executed += 1;
  58. }, { scheduled: false });
  59. this.clock.tick(2000);
  60. assert.equal(0, executed);
  61. });
  62. it('should start a stoped task', () => {
  63. let executed = 0;
  64. let scheduledTask = cron.schedule('* * * * * *', () => {
  65. executed += 1;
  66. }, { scheduled: false });
  67. this.clock.tick(2000);
  68. assert.equal(0, executed);
  69. scheduledTask.start();
  70. this.clock.tick(2000);
  71. assert.equal(2, executed);
  72. });
  73. it('should recover missed executions', (done) => {
  74. let executed = 0;
  75. this.clock.restore();
  76. let scheduledTask = cron.schedule('* * * * * *', () => {
  77. executed += 1;
  78. }, { recoverMissedExecutions: true });
  79. let wait = true;
  80. let startedAt = new Date();
  81. while(wait){
  82. if((new Date().getTime() - startedAt.getTime()) > 1000){
  83. wait = false;
  84. }
  85. }
  86. setTimeout(() => {
  87. scheduledTask.stop();
  88. assert.equal(2, executed);
  89. done();
  90. }, 1000);
  91. }).timeout(4000);
  92. it('should schedule a background task', () => {
  93. let task = cron.schedule('* * * * * *', './test/assets/dummy-task.js');
  94. assert.isNotNull(task);
  95. assert.isDefined(task);
  96. assert.isTrue(task.isRunning());
  97. task.stop();
  98. });
  99. });
  100. describe('validate', () => {
  101. it('should validate a pattern', () => {
  102. assert.isTrue(cron.validate('* * * * * *'));
  103. });
  104. it('should fail with a invalid pattern', () => {
  105. assert.isFalse(cron.validate('62 * * * * *'));
  106. });
  107. });
  108. describe('getTasks', () => {
  109. beforeEach(() => {
  110. global.scheduledTasks = [];
  111. });
  112. it('should store a task', () => {
  113. cron.schedule('* * * * *', () => {});
  114. assert.lengthOf(cron.getTasks(), 1);
  115. });
  116. });
  117. });