Node-Red configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cron.test.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /* eslint-disable no-new */
  2. const sinon = require('sinon');
  3. const cron = require('../lib/cron');
  4. describe('cron', () => {
  5. let clock;
  6. beforeEach(() => {
  7. clock = sinon.useFakeTimers();
  8. });
  9. describe('with seconds', () => {
  10. it('should run every second (* * * * * *)', () => {
  11. const callback = jest.fn();
  12. const job = new cron.CronJob('* * * * * *', callback, null, true);
  13. expect(callback).not.toBeCalled();
  14. clock.tick(1000);
  15. job.stop();
  16. clock.restore();
  17. expect(callback).toHaveBeenCalledTimes(1);
  18. });
  19. it('should run second with oncomplete (* * * * * *)', done => {
  20. const callback = jest.fn();
  21. const job = new cron.CronJob(
  22. '* * * * * *',
  23. callback,
  24. function() {
  25. expect(callback).toHaveBeenCalledTimes(1);
  26. done();
  27. },
  28. true
  29. );
  30. clock.tick(1000);
  31. job.stop();
  32. clock.restore();
  33. });
  34. it('should use standard cron no-seconds syntax (* * * * *)', () => {
  35. const callback = jest.fn();
  36. const job = new cron.CronJob('* * * * *', callback, null, true);
  37. clock.tick(1000); // tick second
  38. clock.tick(59 * 1000); // tick minute
  39. job.stop();
  40. clock.restore();
  41. expect(callback).toHaveBeenCalledTimes(1);
  42. });
  43. it('should run every second for 5 seconds (* * * * * *)', () => {
  44. const callback = jest.fn();
  45. const job = new cron.CronJob('* * * * * *', callback, null, true);
  46. for (var i = 0; i < 5; i++) clock.tick(1000);
  47. job.stop();
  48. clock.restore();
  49. expect(callback).toHaveBeenCalledTimes(5);
  50. });
  51. it('should run every second for 5 seconds with oncomplete (* * * * * *)', done => {
  52. const callback = jest.fn();
  53. const job = new cron.CronJob(
  54. '* * * * * *',
  55. callback,
  56. function() {
  57. expect(callback).toHaveBeenCalledTimes(5);
  58. done();
  59. },
  60. true
  61. );
  62. for (var i = 0; i < 5; i++) clock.tick(1000);
  63. job.stop();
  64. clock.restore();
  65. });
  66. it('should run every second for 5 seconds (*/1 * * * * *)', () => {
  67. const callback = jest.fn();
  68. const job = new cron.CronJob('*/1 * * * * *', callback, null, true);
  69. for (var i = 0; i < 5; i++) clock.tick(1000);
  70. job.stop();
  71. clock.restore();
  72. expect(callback).toHaveBeenCalledTimes(5);
  73. });
  74. it('should run every 2 seconds for 1 seconds (*/2 * * * * *)', () => {
  75. const callback = jest.fn();
  76. const job = new cron.CronJob('*/2 * * * * *', callback, null, true);
  77. clock.tick(1000);
  78. job.stop();
  79. clock.restore();
  80. expect(callback).toHaveBeenCalledTimes(0);
  81. });
  82. it('should run every 2 seconds for 5 seconds (*/2 * * * * *)', () => {
  83. const callback = jest.fn();
  84. const job = new cron.CronJob('*/2 * * * * *', callback, null, true);
  85. for (var i = 0; i < 5; i++) clock.tick(1000);
  86. job.stop();
  87. clock.restore();
  88. expect(callback).toHaveBeenCalledTimes(2);
  89. });
  90. it('should run every second for 5 seconds with oncomplete (*/1 * * * * *)', done => {
  91. const callback = jest.fn();
  92. const job = new cron.CronJob(
  93. '*/1 * * * * *',
  94. callback,
  95. function() {
  96. expect(callback).toHaveBeenCalledTimes(5);
  97. done();
  98. },
  99. true
  100. );
  101. for (var i = 0; i < 5; i++) clock.tick(1000);
  102. job.stop();
  103. clock.restore();
  104. });
  105. it('should run every second for a range ([start]-[end] * * * * *)', () => {
  106. const callback = jest.fn();
  107. const job = new cron.CronJob('0-8 * * * * *', callback, null, true);
  108. clock.tick(10000);
  109. job.stop();
  110. clock.restore();
  111. expect(callback).toHaveBeenCalledTimes(8);
  112. });
  113. it('should run every second for a range ([start]-[end] * * * * *)', done => {
  114. const callback = jest.fn();
  115. const job = new cron.CronJob(
  116. '0-8 * * * * *',
  117. callback,
  118. function() {
  119. expect(callback).toHaveBeenCalledTimes(8);
  120. done();
  121. },
  122. true
  123. );
  124. clock.tick(10000);
  125. job.stop();
  126. clock.restore();
  127. });
  128. it('should default to full range when upper range not provided (1/2 * * * * *)', done => {
  129. const callback = jest.fn();
  130. const job = new cron.CronJob(
  131. '1/2 * * * * *',
  132. callback,
  133. () => {
  134. expect(callback).toHaveBeenCalledTimes(30);
  135. done();
  136. },
  137. true
  138. );
  139. clock.tick(1000 * 60);
  140. job.stop();
  141. clock.restore();
  142. });
  143. it('should run every second (* * * * * *) using the object constructor', () => {
  144. const callback = jest.fn();
  145. const job = new cron.CronJob({
  146. cronTime: '* * * * * *',
  147. onTick: callback,
  148. start: true
  149. });
  150. clock.tick(1000);
  151. job.stop();
  152. clock.restore();
  153. expect(callback).toHaveBeenCalledTimes(1);
  154. });
  155. it('should run every second with oncomplete (* * * * * *) using the object constructor', done => {
  156. const callback = jest.fn();
  157. const job = new cron.CronJob({
  158. cronTime: '* * * * * *',
  159. onTick: callback,
  160. onComplete: function() {
  161. expect(callback).toHaveBeenCalledTimes(1);
  162. done();
  163. },
  164. start: true
  165. });
  166. clock.tick(1000);
  167. job.stop();
  168. clock.restore();
  169. });
  170. });
  171. describe('with minutes', () => {
  172. it('should fire every 60 min', () => {
  173. const m60 = 60 * 60 * 1000;
  174. const l = [];
  175. const job = new cron.CronJob(
  176. '00 30 * * * *',
  177. function() {
  178. l.push(Math.floor(Date.now() / 60000));
  179. },
  180. null,
  181. true
  182. );
  183. clock.tick(m60 * 10);
  184. expect(l.length).toBe(10);
  185. expect(l.every(i => i % 30 === 0)).toBe(true);
  186. job.stop();
  187. clock.restore();
  188. });
  189. it('should run every 45 minutes for 2 hours (0 */45 * * * *)', () => {
  190. const callback = jest.fn();
  191. const job = new cron.CronJob('0 */45 * * * *', callback, null, true);
  192. for (var i = 0; i < 2; i++) clock.tick(60 * 60 * 1000);
  193. job.stop();
  194. clock.restore();
  195. expect(callback).toHaveBeenCalledTimes(4);
  196. });
  197. it('should run every 45 minutes for 2 hours (0 */45 * * * *)', done => {
  198. const callback = jest.fn();
  199. const job = new cron.CronJob(
  200. '0 */45 * * * *',
  201. callback,
  202. function() {
  203. expect(callback).toHaveBeenCalledTimes(4);
  204. done();
  205. },
  206. true
  207. );
  208. for (var i = 0; i < 2; i++) clock.tick(60 * 60 * 1000);
  209. job.stop();
  210. clock.restore();
  211. });
  212. });
  213. it('should start and stop job', done => {
  214. const callback = jest.fn();
  215. const job = new cron.CronJob(
  216. '* * * * * *',
  217. function() {
  218. callback();
  219. this.stop();
  220. },
  221. function() {
  222. expect(callback).toHaveBeenCalledTimes(1);
  223. clock.restore();
  224. done();
  225. },
  226. true
  227. );
  228. clock.tick(1000);
  229. job.stop();
  230. });
  231. describe('with date', () => {
  232. it('should run on a specific date', () => {
  233. const d = new Date();
  234. const clock = sinon.useFakeTimers(d.getTime());
  235. const s = d.getSeconds() + 1;
  236. d.setSeconds(s);
  237. const callback = jest.fn();
  238. const job = new cron.CronJob(
  239. d,
  240. function() {
  241. var t = new Date();
  242. expect(t.getSeconds()).toBe(d.getSeconds());
  243. callback();
  244. },
  245. null,
  246. true
  247. );
  248. clock.tick(1000);
  249. clock.restore();
  250. job.stop();
  251. expect(callback).toHaveBeenCalledTimes(1);
  252. });
  253. it('should run on a specific date with oncomplete', done => {
  254. const d = new Date();
  255. const clock = sinon.useFakeTimers(d.getTime());
  256. const s = d.getSeconds() + 1;
  257. d.setSeconds(s);
  258. const callback = jest.fn();
  259. const job = new cron.CronJob(
  260. d,
  261. function() {
  262. var t = new Date();
  263. expect(t.getSeconds()).toBe(d.getSeconds());
  264. callback();
  265. },
  266. function() {
  267. expect(callback).toHaveBeenCalledTimes(1);
  268. done();
  269. },
  270. true
  271. );
  272. clock.tick(1000);
  273. clock.restore();
  274. job.stop();
  275. });
  276. it('should wait and not fire immediately', function() {
  277. const clock = sinon.useFakeTimers();
  278. const callback = jest.fn();
  279. const d = new Date().getTime() + 31 * 86400 * 1000;
  280. var job = cron.job(new Date(d), callback);
  281. job.start();
  282. clock.tick(1000);
  283. clock.restore();
  284. job.stop();
  285. expect(callback).toHaveBeenCalledTimes(0);
  286. });
  287. });
  288. describe('with timezone', () => {
  289. it('should run a job using cron syntax', function() {
  290. const callback = jest.fn();
  291. const moment = require('moment-timezone');
  292. let zone = 'America/Chicago';
  293. // New Orleans time
  294. const t = moment();
  295. t.tz(zone);
  296. // Current time
  297. const d = moment();
  298. // If current time is New Orleans time, switch to Los Angeles..
  299. if (t.hours() === d.hours()) {
  300. zone = 'America/Los_Angeles';
  301. t.tz(zone);
  302. }
  303. expect(d.hours()).not.toBe(t.hours());
  304. // If t = 59s12m then t.setSeconds(60)
  305. // becomes 00s13m so we're fine just doing
  306. // this and no testRun callback.
  307. t.add(1, 's');
  308. // Run a job designed to be executed at a given
  309. // time in `zone`, making sure that it is a different
  310. // hour than local time.
  311. const job = new cron.CronJob(
  312. t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *',
  313. callback,
  314. null,
  315. true,
  316. zone
  317. );
  318. clock.tick(1000);
  319. clock.restore();
  320. job.stop();
  321. expect(callback).toHaveBeenCalledTimes(1);
  322. });
  323. it('should run a job using a date', function() {
  324. const moment = require('moment-timezone');
  325. let zone = 'America/Chicago';
  326. // New Orleans time
  327. const t = moment();
  328. t.tz(zone);
  329. // Current time
  330. const d = moment();
  331. // If current time is New Orleans time, switch to Los Angeles..
  332. if (t.hours() === d.hours()) {
  333. zone = 'America/Los_Angeles';
  334. t.tz(zone);
  335. }
  336. expect(d.hours()).not.toBe(t.hours());
  337. d.add(1, 'second');
  338. const clock = sinon.useFakeTimers(d.valueOf());
  339. const callback = jest.fn();
  340. const job = new cron.CronJob(d._d, callback, null, true, zone);
  341. clock.tick(1000);
  342. clock.restore();
  343. job.stop();
  344. expect(callback).toHaveBeenCalledTimes(1);
  345. });
  346. it('should test if timezone is valid.', function() {
  347. expect(function() {
  348. // eslint-disable-next-line no-new
  349. new cron.CronJob({
  350. cronTime: '* * * * * *',
  351. onTick: function() {},
  352. timeZone: 'fake/timezone'
  353. });
  354. }).toThrow();
  355. });
  356. });
  357. it('should start, change time, start again', function() {
  358. const callback = jest.fn();
  359. const clock = sinon.useFakeTimers();
  360. const job = new cron.CronJob('* * * * * *', callback);
  361. job.start();
  362. clock.tick(1000);
  363. job.stop();
  364. const time = cron.time('*/2 * * * * *');
  365. job.setTime(time);
  366. job.start();
  367. clock.tick(4000);
  368. clock.restore();
  369. job.stop();
  370. expect(callback).toHaveBeenCalledTimes(3);
  371. });
  372. it('should start, change time, exception', function() {
  373. const callback = jest.fn();
  374. var clock = sinon.useFakeTimers();
  375. var job = new cron.CronJob('* * * * * *', callback);
  376. var time = new Date();
  377. job.start();
  378. clock.tick(1000);
  379. job.stop();
  380. expect(function() {
  381. job.setTime(time);
  382. }).toThrow();
  383. clock.restore();
  384. job.stop();
  385. expect(callback).toHaveBeenCalledTimes(1);
  386. });
  387. it('should scope onTick to running job', function() {
  388. const clock = sinon.useFakeTimers();
  389. const job = new cron.CronJob(
  390. '* * * * * *',
  391. function() {
  392. expect(job).toBeInstanceOf(cron.CronJob);
  393. expect(job).toEqual(this);
  394. },
  395. null,
  396. true
  397. );
  398. clock.tick(1000);
  399. clock.restore();
  400. job.stop();
  401. });
  402. it('should scope onTick to object', function() {
  403. const clock = sinon.useFakeTimers();
  404. const job = new cron.CronJob(
  405. '* * * * * *',
  406. function() {
  407. expect(this.hello).toEqual('world');
  408. expect(job).not.toEqual(this);
  409. },
  410. null,
  411. true,
  412. null,
  413. { hello: 'world' }
  414. );
  415. clock.tick(1000);
  416. clock.restore();
  417. job.stop();
  418. });
  419. it('should scope onTick to object within contstructor object', function() {
  420. const clock = sinon.useFakeTimers();
  421. const job = new cron.CronJob({
  422. cronTime: '* * * * * *',
  423. onTick: function() {
  424. expect(this.hello).toEqual('world');
  425. expect(job).not.toEqual(this);
  426. },
  427. start: true,
  428. context: { hello: 'world' }
  429. });
  430. clock.tick(1000);
  431. clock.restore();
  432. job.stop();
  433. });
  434. it('should not get into an infinite loop on invalid times', function() {
  435. expect(function() {
  436. new cron.CronJob(
  437. '* 60 * * * *',
  438. function() {
  439. expect.ok(true);
  440. },
  441. null,
  442. true
  443. );
  444. }).toThrow();
  445. expect(function() {
  446. new cron.CronJob(
  447. '* * 24 * * *',
  448. function() {
  449. expect.ok(true);
  450. },
  451. null,
  452. true
  453. );
  454. }).toThrow();
  455. });
  456. it('should test start of month', function() {
  457. const callback = jest.fn();
  458. const d = new Date('12/31/2014');
  459. d.setSeconds(59);
  460. d.setMinutes(59);
  461. d.setHours(23);
  462. var clock = sinon.useFakeTimers(d.getTime());
  463. var job = new cron.CronJob('0 0 0 1 * *', callback, null, true);
  464. clock.tick(1001);
  465. expect(callback).toHaveBeenCalledTimes(1);
  466. clock.tick(2678399001);
  467. expect(callback).toHaveBeenCalledTimes(1);
  468. clock.tick(2678400001); // jump over 2 firsts
  469. clock.restore();
  470. job.stop();
  471. expect(callback).toHaveBeenCalledTimes(3);
  472. });
  473. it('should not fire if time was adjusted back', function() {
  474. const callback = jest.fn();
  475. const clock = sinon.useFakeTimers({
  476. toFake: ['setTimeout']
  477. });
  478. const job = new cron.CronJob('0 * * * * *', callback, null, true);
  479. clock.tick(60000);
  480. expect(callback).toHaveBeenCalledTimes(0);
  481. clock.restore();
  482. job.stop();
  483. });
  484. it('should run every day', function() {
  485. const callback = jest.fn();
  486. const d = new Date('12/31/2014');
  487. d.setSeconds(59);
  488. d.setMinutes(59);
  489. d.setHours(23);
  490. var clock = sinon.useFakeTimers(d.getTime());
  491. var job = new cron.CronJob({
  492. cronTime: '59 59 3 * * *',
  493. onTick: callback,
  494. start: true,
  495. timeZone: 'America/Los_Angeles'
  496. });
  497. var twoWeeks = 14 * 24 * 60 * 60 * 1000;
  498. clock.tick(twoWeeks);
  499. clock.restore();
  500. job.stop();
  501. expect(callback).toHaveBeenCalledTimes(14);
  502. });
  503. it('should run every 2 hours between hours', function() {
  504. const callback = jest.fn();
  505. const d = new Date('12/31/2014');
  506. d.setSeconds(0);
  507. d.setMinutes(0);
  508. d.setHours(0);
  509. const clock = sinon.useFakeTimers(d.getTime());
  510. const job = new cron.CronJob({
  511. cronTime: '0 2-6/2 * * * *',
  512. onTick: callback,
  513. start: true
  514. });
  515. clock.tick(2 * 60 * 1000);
  516. expect(callback).toHaveBeenCalledTimes(1);
  517. clock.tick(2 * 60 * 1000);
  518. expect(callback).toHaveBeenCalledTimes(2);
  519. clock.tick(2 * 60 * 1000);
  520. expect(callback).toHaveBeenCalledTimes(3);
  521. clock.tick(2 * 60 * 1000);
  522. clock.restore();
  523. job.stop();
  524. expect(callback).toHaveBeenCalledTimes(3);
  525. });
  526. it('should run every minute', function() {
  527. const callback = jest.fn();
  528. const d = new Date('12/31/2014');
  529. d.setSeconds(0);
  530. d.setMinutes(0);
  531. d.setHours(0);
  532. const clock = sinon.useFakeTimers(d.getTime());
  533. const job = new cron.CronJob({
  534. cronTime: '00 * * * * *',
  535. onTick: callback,
  536. start: true
  537. });
  538. clock.tick(60 * 1000);
  539. expect(callback).toHaveBeenCalledTimes(1);
  540. clock.tick(60 * 1000);
  541. expect(callback).toHaveBeenCalledTimes(2);
  542. clock.restore();
  543. job.stop();
  544. expect(callback).toHaveBeenCalledTimes(2);
  545. });
  546. it('should run every day', function() {
  547. const callback = jest.fn();
  548. const d = new Date('12/31/2014');
  549. d.setSeconds(0);
  550. d.setMinutes(0);
  551. d.setHours(0);
  552. const clock = sinon.useFakeTimers(d.getTime());
  553. const job = new cron.CronJob({
  554. cronTime: '00 30 00 * * *',
  555. onTick: callback,
  556. start: true
  557. });
  558. const day = 24 * 60 * 60 * 1000;
  559. clock.tick(day);
  560. expect(callback).toHaveBeenCalledTimes(1);
  561. clock.tick(day);
  562. expect(callback).toHaveBeenCalledTimes(2);
  563. clock.tick(day);
  564. expect(callback).toHaveBeenCalledTimes(3);
  565. clock.tick(5 * day);
  566. clock.restore();
  567. job.stop();
  568. expect(callback).toHaveBeenCalledTimes(8);
  569. });
  570. it('should trigger onTick at midnight', function() {
  571. const callback = jest.fn();
  572. const d = new Date('12/31/2014');
  573. d.setSeconds(59);
  574. d.setMinutes(59);
  575. d.setHours(23);
  576. const clock = sinon.useFakeTimers(d.getTime());
  577. const job = new cron.CronJob({
  578. cronTime: '00 * * * * *',
  579. onTick: callback,
  580. start: true,
  581. timeZone: 'UTC'
  582. });
  583. clock.tick(1000); // move clock 1 second
  584. expect(callback).toHaveBeenCalledTimes(1);
  585. clock.restore();
  586. job.stop();
  587. expect(callback).toHaveBeenCalledTimes(1);
  588. });
  589. it('should run every day UTC', function() {
  590. const callback = jest.fn();
  591. const d = new Date('12/31/2014');
  592. d.setSeconds(0);
  593. d.setMinutes(0);
  594. d.setHours(0);
  595. const clock = sinon.useFakeTimers(d.getTime());
  596. const job = new cron.CronJob({
  597. cronTime: '00 30 00 * * *',
  598. onTick: callback,
  599. start: true,
  600. timeZone: 'UTC'
  601. });
  602. var day = 24 * 60 * 60 * 1000;
  603. clock.tick(day);
  604. expect(callback).toHaveBeenCalledTimes(1);
  605. clock.tick(day);
  606. expect(callback).toHaveBeenCalledTimes(2);
  607. clock.tick(day);
  608. expect(callback).toHaveBeenCalledTimes(3);
  609. clock.tick(5 * day);
  610. clock.restore();
  611. job.stop();
  612. expect(callback).toHaveBeenCalledTimes(8);
  613. });
  614. // from https://github.com/kelektiv/node-cron/issues/180#issuecomment-154108131
  615. it('should run once not double', function() {
  616. const callback = jest.fn();
  617. const d = new Date(2015, 1, 1, 1, 1, 41, 0);
  618. const clock = sinon.useFakeTimers(d.getTime());
  619. const job = new cron.CronJob({
  620. cronTime: '* * * * *',
  621. onTick: callback,
  622. start: true
  623. });
  624. var minute = 60 * 1000;
  625. clock.tick(minute);
  626. expect(callback).toHaveBeenCalledTimes(1);
  627. clock.restore();
  628. job.stop();
  629. expect(callback).toHaveBeenCalledTimes(1);
  630. });
  631. describe('with utcOffset', function() {
  632. it('should run a job using cron syntax with number format utcOffset', function() {
  633. const clock = sinon.useFakeTimers();
  634. const callback = jest.fn();
  635. const moment = require('moment-timezone');
  636. // Current time
  637. const t = moment();
  638. // UTC Offset decreased by an hour
  639. const utcOffset = t.utcOffset() - 60;
  640. const job = new cron.CronJob(
  641. t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *',
  642. callback,
  643. null,
  644. true,
  645. null,
  646. null,
  647. null,
  648. utcOffset
  649. );
  650. // tick 1 sec before an hour
  651. clock.tick(1000 * 60 * 60 - 1);
  652. expect(callback).toHaveBeenCalledTimes(0);
  653. clock.tick(1);
  654. clock.restore();
  655. job.stop();
  656. expect(callback).toHaveBeenCalledTimes(1);
  657. });
  658. it('should run a job using cron syntax with string format utcOffset', function() {
  659. const clock = sinon.useFakeTimers();
  660. const callback = jest.fn();
  661. const moment = require('moment-timezone');
  662. // Current time
  663. const t = moment();
  664. // UTC Offset decreased by an hour (string format '(+/-)HH:mm')
  665. const utcOffset = t.utcOffset() - 60;
  666. let utcOffsetString = utcOffset > 0 ? '+' : '-';
  667. utcOffsetString += ('0' + Math.floor(Math.abs(utcOffset) / 60)).slice(-2);
  668. utcOffsetString += ':';
  669. utcOffsetString += ('0' + (utcOffset % 60)).slice(-2);
  670. var job = new cron.CronJob(
  671. t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *',
  672. callback,
  673. null,
  674. true,
  675. null,
  676. null,
  677. null,
  678. utcOffsetString
  679. );
  680. // tick 1 sec before an hour
  681. clock.tick(1000 * 60 * 60 - 1);
  682. expect(callback).toHaveBeenCalledTimes(0);
  683. // tick 1 sec
  684. clock.tick(1);
  685. clock.restore();
  686. job.stop();
  687. expect(callback).toHaveBeenCalledTimes(1);
  688. });
  689. it('should run a job using cron syntax with number format utcOffset that is 0', function() {
  690. const clock = sinon.useFakeTimers();
  691. const callback = jest.fn();
  692. const job = new cron.CronJob(
  693. '* * * * * *',
  694. callback,
  695. null,
  696. true,
  697. null,
  698. null,
  699. null,
  700. 0
  701. );
  702. clock.tick(999);
  703. expect(callback).toHaveBeenCalledTimes(0);
  704. clock.tick(1);
  705. clock.restore();
  706. job.stop();
  707. expect(callback).toHaveBeenCalledTimes(1);
  708. });
  709. it('should be able to detect out of range days of month', function() {
  710. expect(function() {
  711. new cron.CronTime('* * 32 FEB *');
  712. }).toThrow();
  713. });
  714. });
  715. });