@@ -17,6 +17,7 @@ import { isChildProcess } from '../../utils/base'
1717import { RealDate , mockDate , resetDate } from './date'
1818
1919export class FakeTimers {
20+ private _global : typeof globalThis
2021 private _clock ! : InstalledClock
2122 private _fakingTime : boolean
2223 private _fakingDate : boolean
@@ -37,6 +38,7 @@ export class FakeTimers {
3738
3839 this . _fakingTime = false
3940 this . _fakeTimers = withGlobal ( global )
41+ this . _global = global
4042 }
4143
4244 clearAllTimers ( ) : void {
@@ -138,13 +140,17 @@ export class FakeTimers {
138140 if ( this . _userConfig ?. toFake ?. includes ( 'nextTick' ) && isChildProcess ( ) )
139141 throw new Error ( 'process.nextTick cannot be mocked inside child_process' )
140142
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
141147 const existingFakedMethods = ( this . _userConfig ?. toFake || toFake ) . filter ( ( method ) => {
142148 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 ]
146152 default :
147- return method in globalThis && globalThis [ method ]
153+ return true
148154 }
149155 } )
150156
0 commit comments