Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions packages/vitest/src/integrations/mock/timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import type {
} from '@sinonjs/fake-timers'
import { withGlobal } from '@sinonjs/fake-timers'
import { isChildProcess } from '../../runtime/utils'
import { mockDate, RealDate, resetDate } from './date'
// import { mockDate, RealDate, resetDate } from './date'

const RealDate = Date

export class FakeTimers {
private _global: typeof globalThis
private _clock!: InstalledClock
private _fakingTime: boolean
private _fakingDate: boolean
// private _fakingDate: boolean
private _fakeTimers: FakeTimerWithContext
private _userConfig?: FakeTimerInstallOpts
private _now = RealDate.now
Expand All @@ -32,7 +34,7 @@ export class FakeTimers {
}) {
this._userConfig = config

this._fakingDate = false
// this._fakingDate = false

this._fakingTime = false
this._fakeTimers = withGlobal(global)
Expand Down Expand Up @@ -127,10 +129,10 @@ export class FakeTimers {
}

useRealTimers(): void {
if (this._fakingDate) {
resetDate()
this._fakingDate = false
}
// if (this._fakingDate) {
// resetDate()
// this._fakingDate = false
// }

if (this._fakingTime) {
this._clock.uninstall()
Expand All @@ -139,11 +141,11 @@ export class FakeTimers {
}

useFakeTimers(): void {
if (this._fakingDate) {
throw new Error(
'"setSystemTime" was called already and date was mocked. Reset timers using `vi.useRealTimers()` if you want to use fake timers again.',
)
}
// if (this._fakingDate) {
// throw new Error(
// '"setSystemTime" was called already and date was mocked. Reset timers using `vi.useRealTimers()` if you want to use fake timers again.',
// )
// }

if (!this._fakingTime) {
const toFake = Object.keys(this._fakeTimers.timers)
Expand Down Expand Up @@ -178,17 +180,20 @@ export class FakeTimers {
}

setSystemTime(now?: number | Date): void {
if (this._fakingTime) {
this._clock.setSystemTime(now)
}
else {
mockDate(now ?? this.getRealSystemTime())
this._fakingDate = true
}
this._checkFakeTimers()
this._clock.setSystemTime(now)
// if (this._fakingTime) {
// this._clock.setSystemTime(now)
// }
// else {
// mockDate(now ?? this.getRealSystemTime())
// this._fakingDate = true
// }
}

getRealSystemTime(): number {
return this._now()
return RealDate.now()
// return this._now()
}

getTimerCount(): number {
Expand Down
7 changes: 6 additions & 1 deletion test/core/test/date-mock.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { afterEach, describe, expect, test, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'

describe('testing date mock functionality', () => {
beforeEach(() => {
vi.useFakeTimers({
toFake: ['Date'],
})
})
afterEach(() => {
vi.useRealTimers()
})
Expand Down
12 changes: 7 additions & 5 deletions test/core/test/fixtures/timers.suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1487,18 +1487,20 @@ describe('FakeTimers', () => {
it('throws when using useFakeTimers after setSystemTime', () => {
const timers = new FakeTimers({ global })

timers.useFakeTimers()

const timeStr = 'Fri Feb 20 2015 19:29:31 GMT+0530'
const timeStrMs = 1424440771000

timers.setSystemTime(timeStr)
// timers.setSystemTime(timeStr)

expect(Date.now()).toBe(timeStrMs)
// expect(Date.now()).toBe(timeStrMs)

expect(() => timers.useFakeTimers()).toThrowError(/date was mocked/)
// // expect(() => timers.useFakeTimers()).toThrowError(/date was mocked/)

// Some test
// // Some test

timers.useRealTimers()
// timers.useRealTimers()
})
})
})
Loading