Skip to content

Commit 9ceb1d7

Browse files
� Fix Vitest fake timer cleanup and module-level state leak (#11178)
Signed-off-by: Copilot <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent ec43866 commit 9ceb1d7

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

web/src/hooks/__tests__/useNightlyE2EData-funcs.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,13 @@ describe('loadCachedData', () => {
7878
it('returns empty guides when localStorage has invalid JSON', () => {
7979
localStorage.setItem(LS_CACHE_KEY, '{not valid json')
8080
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
81-
const result = loadCachedData()
82-
expect(result).toEqual({ guides: [], isDemo: false })
83-
expect(warnSpy).toHaveBeenCalledOnce()
81+
try {
82+
const result = loadCachedData()
83+
expect(result).toEqual({ guides: [], isDemo: false })
84+
expect(warnSpy).toHaveBeenCalledOnce()
85+
} finally {
86+
warnSpy.mockRestore()
87+
}
8488
})
8589

8690
it('returns empty guides when cached data has no guides property', () => {

web/src/hooks/__tests__/useTokenUsage-funcs.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, vi, beforeEach } from 'vitest'
1+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
22
import { __testables } from '../useTokenUsage'
33

44
const {
@@ -196,6 +196,10 @@ describe('persistUsage', () => {
196196
})
197197

198198
describe('getNextResetDate', () => {
199+
afterEach(() => {
200+
vi.useRealTimers()
201+
})
202+
199203
it('returns a valid ISO date string', () => {
200204
const result = getNextResetDate()
201205
expect(() => new Date(result)).not.toThrow()
@@ -232,7 +236,6 @@ describe('getNextResetDate', () => {
232236
expect(parsed.getFullYear()).toBe(2026)
233237
expect(parsed.getMonth()).toBe(0)
234238
expect(parsed.getDate()).toBe(1)
235-
vi.useRealTimers()
236239
})
237240

238241
it('handles January correctly', () => {
@@ -243,7 +246,6 @@ describe('getNextResetDate', () => {
243246
expect(parsed.getFullYear()).toBe(2025)
244247
expect(parsed.getMonth()).toBe(1)
245248
expect(parsed.getDate()).toBe(1)
246-
vi.useRealTimers()
247249
})
248250

249251
it('handles last day of month', () => {
@@ -254,7 +256,6 @@ describe('getNextResetDate', () => {
254256
expect(parsed.getFullYear()).toBe(2025)
255257
expect(parsed.getMonth()).toBe(3)
256258
expect(parsed.getDate()).toBe(1)
257-
vi.useRealTimers()
258259
})
259260

260261
it('handles first day of month', () => {
@@ -265,6 +266,5 @@ describe('getNextResetDate', () => {
265266
expect(parsed.getFullYear()).toBe(2025)
266267
expect(parsed.getMonth()).toBe(6)
267268
expect(parsed.getDate()).toBe(1)
268-
vi.useRealTimers()
269269
})
270270
})

web/src/hooks/mcp/__tests__/shared-cache-fetch.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ import {
103103
clearClusterFailure,
104104
clusterDisplayName,
105105
fetchWithRetry,
106+
_resetAgentTokenState,
106107
// Async functions
107108
fullFetchClusters,
108109
refreshSingleCluster,
@@ -631,6 +632,7 @@ describe('fetchSingleClusterHealth', () => {
631632
const originalFetch = globalThis.fetch
632633

633634
beforeEach(() => {
635+
_resetAgentTokenState()
634636
mockIsAgentUnavailable.mockReturnValue(false)
635637
mockIsNetlifyDeployment.value = false
636638
mockIsDemoToken.mockReturnValue(false)
@@ -640,6 +642,7 @@ describe('fetchSingleClusterHealth', () => {
640642
afterEach(() => {
641643
globalThis.fetch = originalFetch
642644
vi.restoreAllMocks()
645+
_resetAgentTokenState()
643646
})
644647

645648
it('returns health data from agent HTTP endpoint', async () => {

web/src/hooks/mcp/__tests__/shared-coverage.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ describe('getAgentToken — emits GA4 on failure', () => {
233233
globalThis.fetch = originalFetch
234234
localStorage.clear()
235235
mockEmitAgentTokenFailure.mockClear()
236+
_resetAgentTokenState()
236237
})
237238

238239
it('emits emitAgentTokenFailure when /api/agent/token returns non-OK', async () => {

0 commit comments

Comments
 (0)