Skip to content

Commit c047865

Browse files
committed
wip
1 parent c82387d commit c047865

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

packages/vitest/src/integrations/mock/timers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import { mockDate, RealDate, resetDate } from './date'
1717
export class FakeTimers {
1818
private _global: typeof globalThis
1919
private _clock!: InstalledClock
20+
// | _fakingDate | _fakingTime |
21+
// +-------------+-------------+
22+
// | false | false | initial
23+
// | true | false | vi.setSystemTime called without vi.useFakeTimers
24+
// | false | true | vi.useFakeTimers called
25+
// | true | true | unreachable
2026
private _fakingTime: boolean
2127
private _fakingDate: boolean
2228
private _fakeTimers: FakeTimerWithContext
@@ -187,6 +193,10 @@ export class FakeTimers {
187193
}
188194
}
189195

196+
getMockedSystemTime(): number | null {
197+
return this._fakingTime ? this._clock.now : null
198+
}
199+
190200
getRealSystemTime(): number {
191201
return this._now()
192202
}

packages/vitest/src/integrations/vi.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface VitestUtils {
8585
*/
8686
setSystemTime: (time: number | string | Date) => VitestUtils
8787
/**
88-
* Returns mocked current date that was set using `setSystemTime`. If date is not mocked the method will return `null`.
88+
* Returns mocked current date. If date is not mocked the method will return `null`.
8989
*/
9090
getMockedSystemTime: () => Date | null
9191
/**
@@ -508,8 +508,10 @@ function createVitest(): VitestUtils {
508508
return utils
509509
},
510510

511+
// TODO: probably we can return `number` to be consistent with `getRealSystemTime`
511512
getMockedSystemTime() {
512-
return _mockedDate
513+
const time = timers().getMockedSystemTime()
514+
return time === null ? null : new Date(time)
513515
},
514516

515517
getRealSystemTime() {

0 commit comments

Comments
 (0)