• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

node-cron / node-cron / 14765410028

30 Apr 2025 10:16PM UTC coverage: 91.876% (-2.0%) from 93.838%
14765410028

push

github

merencia
adding features

524 of 618 branches covered (84.79%)

Branch coverage included in aggregate %.

35 of 81 new or added lines in 3 files covered. (43.21%)

10 existing lines in 4 files now uncovered.

1783 of 1893 relevant lines covered (94.19%)

29186.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.59
/src/node-cron.test.ts
1
import { assert }  from 'chai';
1!
2
import { useFakeTimers } from 'sinon';
1✔
3
import cron from './node-cron';
1✔
4

1✔
5
let clock;
1✔
6

1✔
7
describe('node-cron', function() {
1✔
8
    beforeEach(function() {
1✔
9
        clock = useFakeTimers(new Date(2018, 0, 1, 0, 0, 0, 0));
10✔
10
    });
1✔
11
    
1✔
12
    afterEach(function() {
1✔
13
        clock.restore();
10✔
14
    });
1✔
15
    
1✔
16
    describe('schedule', function() {
1✔
17
        it('should schedule a task', function() {
1✔
18
            let executed = 0;
1✔
19
            const task = cron.schedule('* * * * * *', () => {
1✔
20
                executed += 1;
2✔
21
            });
1✔
22
            
1✔
23
            clock.tick(2000);
1✔
24
            
1✔
25
            assert.equal(2, executed);
1✔
26
            task.stop();
1✔
27
        });
1✔
28
        
1✔
29
        it('should schedule a task with America/Sao_Paulo timezone', function(done) {
1✔
30
            let startDate = new Date('Thu, 20 Sep 2018 00:00:00.000Z');
1✔
31
            clock.restore();
1✔
32
            clock = useFakeTimers(startDate);
1✔
33
            const task = cron.schedule('* * * * * *', (event) => {
1✔
34
                assert.equal(19, event.date.getDate());
1✔
35
                assert.equal(8, event.date.getMonth());
1✔
36
                assert.equal(2018, event.date.getFullYear());
1✔
37
                assert.equal(21, event.date.getHours());
1✔
38
                assert.equal(0, event.date.getMinutes());
1✔
39
                assert.equal(1, event.date.getSeconds());
1✔
40
                done();
1✔
41
            }, {
1✔
42
                timezone: 'America/Sao_Paulo'
1✔
43
            });
1✔
44
            clock.tick(1000);
1✔
45
            task.stop();
1✔
46
        });
1✔
47
        
1✔
48
        it('should schedule a task with Europe/Rome timezone', function(done) {
1✔
49
            let startDate = new Date('Thu, 20 Sep 2018 00:00:00.000Z');
1✔
50
            clock.restore();
1✔
51
            clock = useFakeTimers(startDate);
1✔
52
            const task = cron.schedule('* * * * * *', (event) => {
1✔
53
                assert.equal('2018-09-20T02:00:01.000+02:00', event.dateLocalIso);
1✔
54
                done();
1✔
55
            }, {
1✔
56
                timezone: 'Europe/Rome'
1✔
57
            });
1✔
58
            clock.tick(1000);
1✔
59
            task.stop();
1✔
60
        });
1✔
61
        
1✔
62
        it('should schedule a task stopped', function() {
1✔
63
            let executed = 0;
1✔
64
            const task = cron.schedule('* * * * * *', () => {
1✔
UNCOV
65
                executed += 1;
×
66
            }, { scheduled: false });
1✔
67
            
1✔
68
            clock.tick(2000);
1✔
69
            
1✔
70
            assert.equal(0, executed);
1✔
71
            task.stop();
1✔
72
        });
1✔
73
        
1✔
74
        it('should start a stopped task', function() {
1✔
75
            let executed = 0;
1✔
76
            let task = cron.schedule('* * * * * *', () => {
1✔
77
                executed += 1;
2✔
78
            }, { scheduled: false });
1✔
79
            
1✔
80
            clock.tick(2000);
1✔
81
            assert.equal(0, executed);
1✔
82
            task.start();
1✔
83
            clock.tick(2000);
1✔
84
            assert.equal(2, executed);
1✔
85
            task.stop();
1✔
86
        });
1✔
87
        
1✔
88
        it('should recover missed executions', function(done) {
1✔
89
            let executed = 0;
1✔
90
            clock.restore();
1✔
91
            let task = cron.schedule('* * * * * *', () => {
1✔
92
                executed += 1;
2✔
93
            }, { catchUp: true });
1✔
94
            
1✔
95
            let wait = true;
1✔
96
            let startedAt = new Date();
1✔
97
            
1✔
98
            while(wait){
1✔
99
                if((new Date().getTime() - startedAt.getTime()) > 1000){
9,174,124✔
100
                    wait = false;
1✔
101
                }
1✔
102
            }
9,174,124✔
103
            
1✔
104
            setTimeout(() => {
1✔
105
                task.stop();
1✔
106
                assert.equal(2, executed);
1✔
107
                done();
1✔
108
            }, 1000);
1✔
109
        }).timeout(4000);
1✔
110

1✔
111
        it('should schedule a background task', function() {
1✔
112
            let task = cron.schedule('* * * * * *', './test-assets/dummy-task');
1✔
113
            assert.isNotNull(task);
1✔
114
            assert.isDefined(task);
1✔
115
            task.stop();
1✔
116
        });
1✔
117
    });
1✔
118
    
1✔
119
    describe('validate', function() {
1✔
120
        it('should validate a pattern', function() {
1✔
121
            assert.isTrue(cron.validate('* * * * * *')); 
1✔
122
        });
1✔
123
        
1✔
124
        it('should fail with a invalid pattern', function() {
1✔
125
            assert.isFalse(cron.validate('62 * * * * *')); 
1✔
126
        });
1✔
127
    });
1✔
128

1✔
129
    describe('getTasks', function() {
1✔
130
        it('should store a task', function() {
1✔
131
            const task = cron.schedule('* * * * *', () => {});
1✔
132
            assert.isTrue(cron.getTasks().length > 0);
1✔
133
        });
1✔
134
    });
1✔
135
});
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc