File tree 2 files changed +25
-4
lines changed
packages/vitest/src/integrations/mock
2 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import { isChildProcess } from '../../utils/base'
17
17
import { RealDate , mockDate , resetDate } from './date'
18
18
19
19
export class FakeTimers {
20
+ private _global : typeof globalThis
20
21
private _clock ! : InstalledClock
21
22
private _fakingTime : boolean
22
23
private _fakingDate : boolean
@@ -37,6 +38,7 @@ export class FakeTimers {
37
38
38
39
this . _fakingTime = false
39
40
this . _fakeTimers = withGlobal ( global )
41
+ this . _global = global
40
42
}
41
43
42
44
clearAllTimers ( ) : void {
@@ -138,13 +140,17 @@ export class FakeTimers {
138
140
if ( this . _userConfig ?. toFake ?. includes ( 'nextTick' ) && isChildProcess ( ) )
139
141
throw new Error ( 'process.nextTick cannot be mocked inside child_process' )
140
142
143
+ // setImmediate/clearImmediate is not possible to mock when it's not globally avaiable and it throws an internal error.
144
+ // this might be @sinonjs /fake-timers's bug and inconsistent behavior, but for now, we silently filter out these two beforehand for browser testing.
145
+ // https://github.com/sinonjs/fake-timers/issues/277
146
+ // https://github.com/sinonjs/sinon/issues/2085
141
147
const existingFakedMethods = ( this . _userConfig ?. toFake || toFake ) . filter ( ( method ) => {
142
148
switch ( method ) {
143
- case 'hrtime ' :
144
- case 'nextTick ' :
145
- return typeof process !== 'undefined' && method in process && process [ method ]
149
+ case 'setImmediate ' :
150
+ case 'clearImmediate ' :
151
+ return method in this . _global && this . _global [ method ]
146
152
default :
147
- return method in globalThis && globalThis [ method ]
153
+ return true
148
154
}
149
155
} )
150
156
Original file line number Diff line number Diff line change @@ -119,6 +119,21 @@ describe('FakeTimers', () => {
119
119
timers . useFakeTimers ( )
120
120
expect ( global . clearImmediate ) . not . toBe ( origClearImmediate )
121
121
} )
122
+
123
+ it ( 'mocks requestIdleCallback even if not on global' , ( ) => {
124
+ const global = { Date : FakeDate , clearTimeout, setTimeout } ;
125
+ const timers = new FakeTimers ( { global, config : { toFake : [ "requestIdleCallback" ] } } )
126
+ timers . useFakeTimers ( )
127
+ expect ( global . requestIdleCallback ) . toBeDefined ( ) ;
128
+ } )
129
+
130
+ it ( 'cannot mock setImmediate and clearImmediate if not on global' , ( ) => {
131
+ const global = { Date : FakeDate , clearTimeout, setTimeout } ;
132
+ const timers = new FakeTimers ( { global, config : { toFake : [ "setImmediate" , "clearImmediate" ] } } )
133
+ timers . useFakeTimers ( )
134
+ expect ( global . setImmediate ) . toBeUndefined ( ) ;
135
+ expect ( global . clearImmediate ) . toBeUndefined ( ) ;
136
+ } )
122
137
} )
123
138
124
139
describe ( 'runAllTicks' , ( ) => {
You can’t perform that action at this time.
0 commit comments