MCPcopy
hub / github.com/vitest-dev/vitest / useFakeTimers

Method useFakeTimers

packages/vitest/src/integrations/mock/timers.ts:146–177  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

144 }
145
146 useFakeTimers(): void {
147 const fakeDate = this._fakingDate || Date.now()
148 if (this._fakingDate) {
149 resetDate()
150 this._fakingDate = null
151 }
152
153 if (this._fakingTime) {
154 this._clock.uninstall()
155 }
156
157 const toFake = Object.keys(this._fakeTimers.timers)
158 // Do not mock timers internally used by node by default. It can still be mocked through userConfig.
159 .filter(
160 timer => timer !== 'nextTick' && timer !== 'queueMicrotask',
161 ) as (keyof FakeTimerWithContext['timers'])[]
162
163 if (this._userConfig?.toFake?.includes('nextTick') && isChildProcess()) {
164 throw new Error(
165 'process.nextTick cannot be mocked inside child_process',
166 )
167 }
168
169 this._clock = this._fakeTimers.install({
170 now: fakeDate,
171 ...this._userConfig,
172 toFake: this._userConfig?.toFake || toFake,
173 ignoreMissingTimers: true,
174 })
175
176 this._fakingTime = true
177 }
178
179 reset(): void {
180 if (this._checkFakeTimers()) {

Callers 13

useFakeTimersFunction · 0.80
timers.test.tsFile · 0.80
logs.test.tsFile · 0.80
rpc.spec.tsFile · 0.80
wait.test.tsFile · 0.80
happy-dom.spec.tsFile · 0.80
timers.suite.tsFile · 0.80

Calls 4

resetDateFunction · 0.90
isChildProcessFunction · 0.90
keysMethod · 0.80
filterMethod · 0.65

Tested by

no test coverage detected