11import * as SentryCore from '@sentry/core' ;
22
3+ import { type MockInstance , afterEach , beforeEach , describe , expect , test , vi } from 'vitest' ;
34import { cron } from '../src' ;
45import type { CronJob , CronJobParams } from '../src/cron/cron' ;
56import type { NodeCron , NodeCronOptions } from '../src/cron/node-cron' ;
67
78describe ( 'cron check-ins' , ( ) => {
8- let withMonitorSpy : jest . SpyInstance ;
9+ let withMonitorSpy : MockInstance ;
910
1011 beforeEach ( ( ) => {
11- withMonitorSpy = jest . spyOn ( SentryCore , 'withMonitor' ) ;
12+ withMonitorSpy = vi . spyOn ( SentryCore , 'withMonitor' ) ;
1213 } ) ;
1314
1415 afterEach ( ( ) => {
15- jest . restoreAllMocks ( ) ;
16+ vi . restoreAllMocks ( ) ;
1617 } ) ;
1718
1819 describe ( 'cron' , ( ) => {
@@ -48,43 +49,45 @@ describe('cron check-ins', () => {
4849 }
4950 }
5051
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+ } ) ) ;
7173
72- test ( 'CronJob.from()' , done => {
73- expect . assertions ( 4 ) ;
74+ test ( 'CronJob.from()' , ( ) =>
75+ new Promise < void > ( done => {
76+ expect . assertions ( 4 ) ;
7477
75- const CronJobWithCheckIn = cron . instrumentCron ( CronJobMock , 'my-cron-job' ) ;
78+ const CronJobWithCheckIn = cron . instrumentCron ( CronJobMock , 'my-cron-job' ) ;
7679
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+ } ) ) ;
8891
8992 test ( 'throws with multiple jobs same name' , ( ) => {
9093 const CronJobWithCheckIn = cron . instrumentCron ( CronJobMock , 'my-cron-job' ) ;
@@ -108,32 +111,33 @@ describe('cron check-ins', () => {
108111 } ) ;
109112
110113 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+ } ) ) ;
137141
138142 test ( 'throws without supplied name' , ( ) => {
139143 const nodeCron : NodeCron = {
@@ -154,32 +158,33 @@ describe('cron check-ins', () => {
154158 } ) ;
155159
156160 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+ }
170176 }
171- }
172177
173- const scheduleWithCheckIn = cron . instrumentNodeSchedule ( new NodeScheduleMock ( ) ) ;
178+ const scheduleWithCheckIn = cron . instrumentNodeSchedule ( new NodeScheduleMock ( ) ) ;
174179
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 ( ) ;
179186 } ) ;
180- done ( ) ;
181- } ) ;
182- } ) ;
187+ } ) ) ;
183188
184189 test ( 'throws without crontab string' , ( ) => {
185190 class NodeScheduleMock {
0 commit comments