1
1
import * as SentryCore from '@sentry/core' ;
2
2
3
+ import { type MockInstance , afterEach , beforeEach , describe , expect , test , vi } from 'vitest' ;
3
4
import { cron } from '../src' ;
4
5
import type { CronJob , CronJobParams } from '../src/cron/cron' ;
5
6
import type { NodeCron , NodeCronOptions } from '../src/cron/node-cron' ;
6
7
7
8
describe ( 'cron check-ins' , ( ) => {
8
- let withMonitorSpy : jest . SpyInstance ;
9
+ let withMonitorSpy : MockInstance ;
9
10
10
11
beforeEach ( ( ) => {
11
- withMonitorSpy = jest . spyOn ( SentryCore , 'withMonitor' ) ;
12
+ withMonitorSpy = vi . spyOn ( SentryCore , 'withMonitor' ) ;
12
13
} ) ;
13
14
14
15
afterEach ( ( ) => {
15
- jest . restoreAllMocks ( ) ;
16
+ vi . restoreAllMocks ( ) ;
16
17
} ) ;
17
18
18
19
describe ( 'cron' , ( ) => {
@@ -48,43 +49,45 @@ describe('cron check-ins', () => {
48
49
}
49
50
}
50
51
51
- test ( 'new CronJob()' , done => {
52
- expect . assertions ( 4 ) ;
53
-
54
- const CronJobWithCheckIn = cron . instrumentCron ( CronJobMock , 'my-cron-job' ) ;
55
-
56
- new CronJobWithCheckIn (
57
- '* * * Jan,Sep Sun' ,
58
- ( ) => {
59
- expect ( withMonitorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
60
- expect ( withMonitorSpy ) . toHaveBeenLastCalledWith ( 'my-cron-job' , expect . anything ( ) , {
61
- schedule : { type : 'crontab' , value : '* * * 1,9 0' } ,
62
- timezone : 'America/Los_Angeles' ,
63
- } ) ;
64
- done ( ) ;
65
- } ,
66
- undefined ,
67
- true ,
68
- 'America/Los_Angeles' ,
69
- ) ;
70
- } ) ;
52
+ test ( 'new CronJob()' , ( ) =>
53
+ new Promise < void > ( done => {
54
+ expect . assertions ( 4 ) ;
55
+
56
+ const CronJobWithCheckIn = cron . instrumentCron ( CronJobMock , 'my-cron-job' ) ;
57
+
58
+ new CronJobWithCheckIn (
59
+ '* * * Jan,Sep Sun' ,
60
+ ( ) => {
61
+ expect ( withMonitorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
62
+ expect ( withMonitorSpy ) . toHaveBeenLastCalledWith ( 'my-cron-job' , expect . anything ( ) , {
63
+ schedule : { type : 'crontab' , value : '* * * 1,9 0' } ,
64
+ timezone : 'America/Los_Angeles' ,
65
+ } ) ;
66
+ done ( ) ;
67
+ } ,
68
+ undefined ,
69
+ true ,
70
+ 'America/Los_Angeles' ,
71
+ ) ;
72
+ } ) ) ;
71
73
72
- test ( 'CronJob.from()' , done => {
73
- expect . assertions ( 4 ) ;
74
+ test ( 'CronJob.from()' , ( ) =>
75
+ new Promise < void > ( done => {
76
+ expect . assertions ( 4 ) ;
74
77
75
- const CronJobWithCheckIn = cron . instrumentCron ( CronJobMock , 'my-cron-job' ) ;
78
+ const CronJobWithCheckIn = cron . instrumentCron ( CronJobMock , 'my-cron-job' ) ;
76
79
77
- CronJobWithCheckIn . from ( {
78
- cronTime : '* * * Jan,Sep Sun' ,
79
- onTick : ( ) => {
80
- expect ( withMonitorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
81
- expect ( withMonitorSpy ) . toHaveBeenLastCalledWith ( 'my-cron-job' , expect . anything ( ) , {
82
- schedule : { type : 'crontab' , value : '* * * 1,9 0' } ,
83
- } ) ;
84
- done ( ) ;
85
- } ,
86
- } ) ;
87
- } ) ;
80
+ CronJobWithCheckIn . from ( {
81
+ cronTime : '* * * Jan,Sep Sun' ,
82
+ onTick : ( ) => {
83
+ expect ( withMonitorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
84
+ expect ( withMonitorSpy ) . toHaveBeenLastCalledWith ( 'my-cron-job' , expect . anything ( ) , {
85
+ schedule : { type : 'crontab' , value : '* * * 1,9 0' } ,
86
+ } ) ;
87
+ done ( ) ;
88
+ } ,
89
+ } ) ;
90
+ } ) ) ;
88
91
89
92
test ( 'throws with multiple jobs same name' , ( ) => {
90
93
const CronJobWithCheckIn = cron . instrumentCron ( CronJobMock , 'my-cron-job' ) ;
@@ -108,32 +111,33 @@ describe('cron check-ins', () => {
108
111
} ) ;
109
112
110
113
describe ( 'node-cron' , ( ) => {
111
- test ( 'calls withMonitor' , done => {
112
- expect . assertions ( 5 ) ;
113
-
114
- const nodeCron : NodeCron = {
115
- schedule : ( expression : string , callback : ( ) => void , options ?: NodeCronOptions ) : unknown => {
116
- expect ( expression ) . toBe ( '* * * Jan,Sep Sun' ) ;
117
- expect ( callback ) . toBeInstanceOf ( Function ) ;
118
- expect ( options ?. name ) . toBe ( 'my-cron-job' ) ;
119
- return callback ( ) ;
120
- } ,
121
- } ;
122
-
123
- const cronWithCheckIn = cron . instrumentNodeCron ( nodeCron ) ;
124
-
125
- cronWithCheckIn . schedule (
126
- '* * * Jan,Sep Sun' ,
127
- ( ) => {
128
- expect ( withMonitorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
129
- expect ( withMonitorSpy ) . toHaveBeenLastCalledWith ( 'my-cron-job' , expect . anything ( ) , {
130
- schedule : { type : 'crontab' , value : '* * * 1,9 0' } ,
131
- } ) ;
132
- done ( ) ;
133
- } ,
134
- { name : 'my-cron-job' } ,
135
- ) ;
136
- } ) ;
114
+ test ( 'calls withMonitor' , ( ) =>
115
+ new Promise < void > ( done => {
116
+ expect . assertions ( 5 ) ;
117
+
118
+ const nodeCron : NodeCron = {
119
+ schedule : ( expression : string , callback : ( ) => void , options ?: NodeCronOptions ) : unknown => {
120
+ expect ( expression ) . toBe ( '* * * Jan,Sep Sun' ) ;
121
+ expect ( callback ) . toBeInstanceOf ( Function ) ;
122
+ expect ( options ?. name ) . toBe ( 'my-cron-job' ) ;
123
+ return callback ( ) ;
124
+ } ,
125
+ } ;
126
+
127
+ const cronWithCheckIn = cron . instrumentNodeCron ( nodeCron ) ;
128
+
129
+ cronWithCheckIn . schedule (
130
+ '* * * Jan,Sep Sun' ,
131
+ ( ) => {
132
+ expect ( withMonitorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
133
+ expect ( withMonitorSpy ) . toHaveBeenLastCalledWith ( 'my-cron-job' , expect . anything ( ) , {
134
+ schedule : { type : 'crontab' , value : '* * * 1,9 0' } ,
135
+ } ) ;
136
+ done ( ) ;
137
+ } ,
138
+ { name : 'my-cron-job' } ,
139
+ ) ;
140
+ } ) ) ;
137
141
138
142
test ( 'throws without supplied name' , ( ) => {
139
143
const nodeCron : NodeCron = {
@@ -154,32 +158,33 @@ describe('cron check-ins', () => {
154
158
} ) ;
155
159
156
160
describe ( 'node-schedule' , ( ) => {
157
- test ( 'calls withMonitor' , done => {
158
- expect . assertions ( 5 ) ;
159
-
160
- class NodeScheduleMock {
161
- scheduleJob (
162
- nameOrExpression : string | Date | object ,
163
- expressionOrCallback : string | Date | object | ( ( ) => void ) ,
164
- callback : ( ) => void ,
165
- ) : unknown {
166
- expect ( nameOrExpression ) . toBe ( 'my-cron-job' ) ;
167
- expect ( expressionOrCallback ) . toBe ( '* * * Jan,Sep Sun' ) ;
168
- expect ( callback ) . toBeInstanceOf ( Function ) ;
169
- return callback ( ) ;
161
+ test ( 'calls withMonitor' , ( ) =>
162
+ new Promise < void > ( done => {
163
+ expect . assertions ( 5 ) ;
164
+
165
+ class NodeScheduleMock {
166
+ scheduleJob (
167
+ nameOrExpression : string | Date | object ,
168
+ expressionOrCallback : string | Date | object | ( ( ) => void ) ,
169
+ callback : ( ) => void ,
170
+ ) : unknown {
171
+ expect ( nameOrExpression ) . toBe ( 'my-cron-job' ) ;
172
+ expect ( expressionOrCallback ) . toBe ( '* * * Jan,Sep Sun' ) ;
173
+ expect ( callback ) . toBeInstanceOf ( Function ) ;
174
+ return callback ( ) ;
175
+ }
170
176
}
171
- }
172
177
173
- const scheduleWithCheckIn = cron . instrumentNodeSchedule ( new NodeScheduleMock ( ) ) ;
178
+ const scheduleWithCheckIn = cron . instrumentNodeSchedule ( new NodeScheduleMock ( ) ) ;
174
179
175
- scheduleWithCheckIn . scheduleJob ( 'my-cron-job' , '* * * Jan,Sep Sun' , ( ) => {
176
- expect ( withMonitorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
177
- expect ( withMonitorSpy ) . toHaveBeenLastCalledWith ( 'my-cron-job' , expect . anything ( ) , {
178
- schedule : { type : 'crontab' , value : '* * * 1,9 0' } ,
180
+ scheduleWithCheckIn . scheduleJob ( 'my-cron-job' , '* * * Jan,Sep Sun' , ( ) => {
181
+ expect ( withMonitorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
182
+ expect ( withMonitorSpy ) . toHaveBeenLastCalledWith ( 'my-cron-job' , expect . anything ( ) , {
183
+ schedule : { type : 'crontab' , value : '* * * 1,9 0' } ,
184
+ } ) ;
185
+ done ( ) ;
179
186
} ) ;
180
- done ( ) ;
181
- } ) ;
182
- } ) ;
187
+ } ) ) ;
183
188
184
189
test ( 'throws without crontab string' , ( ) => {
185
190
class NodeScheduleMock {
0 commit comments