Skip to content

Commit

Permalink
fix(vitest): "vi" doesn't rely on context (#4031)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Aug 28, 2023
1 parent 9f04386 commit 0d0f35f
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions packages/vitest/src/integrations/vi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function createVitest(): VitestUtils {
return stack?.file || ''
}

return {
const utils: VitestUtils = {
useFakeTimers(config?: FakeTimerInstallOpts) {
if (config) {
_timers.configure(config)
Expand All @@ -210,58 +210,58 @@ function createVitest(): VitestUtils {
_timers.configure(workerState.config.fakeTimers)
}
_timers.useFakeTimers()
return this
return utils
},

useRealTimers() {
_timers.useRealTimers()
_mockedDate = null
return this
return utils
},

runOnlyPendingTimers() {
_timers.runOnlyPendingTimers()
return this
return utils
},

async runOnlyPendingTimersAsync() {
await _timers.runOnlyPendingTimersAsync()
return this
return utils
},

runAllTimers() {
_timers.runAllTimers()
return this
return utils
},

async runAllTimersAsync() {
await _timers.runAllTimersAsync()
return this
return utils
},

runAllTicks() {
_timers.runAllTicks()
return this
return utils
},

advanceTimersByTime(ms: number) {
_timers.advanceTimersByTime(ms)
return this
return utils
},

async advanceTimersByTimeAsync(ms: number) {
await _timers.advanceTimersByTimeAsync(ms)
return this
return utils
},

advanceTimersToNextTimer() {
_timers.advanceTimersToNextTimer()
return this
return utils
},

async advanceTimersToNextTimerAsync() {
await _timers.advanceTimersToNextTimerAsync()
return this
return utils
},

getTimerCount() {
Expand All @@ -272,7 +272,7 @@ function createVitest(): VitestUtils {
const date = time instanceof Date ? time : new Date(time)
_mockedDate = date
_timers.setSystemTime(date)
return this
return utils
},

getMockedSystemTime() {
Expand All @@ -285,7 +285,7 @@ function createVitest(): VitestUtils {

clearAllTimers() {
_timers.clearAllTimers()
return this
return utils
},

// mocks
Expand Down Expand Up @@ -337,17 +337,17 @@ function createVitest(): VitestUtils {

clearAllMocks() {
spies.forEach(spy => spy.mockClear())
return this
return utils
},

resetAllMocks() {
spies.forEach(spy => spy.mockReset())
return this
return utils
},

restoreAllMocks() {
spies.forEach(spy => spy.mockRestore())
return this
return utils
},

stubGlobal(name: string | symbol | number, value: any) {
Expand All @@ -359,14 +359,14 @@ function createVitest(): VitestUtils {
configurable: true,
enumerable: true,
})
return this
return utils
},

stubEnv(name: string, value: string) {
if (!_stubsEnv.has(name))
_stubsEnv.set(name, process.env[name])
process.env[name] = value
return this
return utils
},

unstubAllGlobals() {
Expand All @@ -377,7 +377,7 @@ function createVitest(): VitestUtils {
Object.defineProperty(globalThis, name, original)
})
_stubsGlobal.clear()
return this
return utils
},

unstubAllEnvs() {
Expand All @@ -388,13 +388,13 @@ function createVitest(): VitestUtils {
process.env[name] = original
})
_stubsEnv.clear()
return this
return utils
},

resetModules() {
const state = getWorkerState()
resetModules(state.moduleCache)
return this
return utils
},

async dynamicImportSettled() {
Expand All @@ -414,8 +414,9 @@ function createVitest(): VitestUtils {
Object.assign(state.config, _config)
}
},

}

return utils
}

export const vitest = createVitest()
Expand Down

0 comments on commit 0d0f35f

Please sign in to comment.