MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / createMockTimers

Function createMockTimers

common/src/testing/mocks/timers.ts:28–125  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26
27/** @deprecated Use Bun's built-in mock.setSystemTime() instead. */
28export function createMockTimers(): MockTimers {
29 const pendingTimers: PendingTimer[] = []
30 let nextId = 1
31 let currentTime = 0
32
33 const originalSetTimeout = globalThis.setTimeout
34 const originalClearTimeout = globalThis.clearTimeout
35
36 const mockSetTimeout = ((fn: () => void, ms?: number): number => {
37 const id = nextId++
38 pendingTimers.push({
39 id,
40 ms: Number(ms ?? 0),
41 fn,
42 active: true,
43 createdAt: currentTime,
44 })
45 return id
46 }) as typeof globalThis.setTimeout
47
48 const mockClearTimeout = ((id?: number): void => {
49 if (id === undefined) return
50 const timer = pendingTimers.find((t) => t.id === id)
51 if (timer) {
52 timer.active = false
53 }
54 }) as typeof globalThis.clearTimeout
55
56 const getActivePending = (): PendingTimer[] => {
57 return pendingTimers.filter((t) => t.active)
58 }
59
60 return {
61 setTimeout: mockSetTimeout,
62 clearTimeout: mockClearTimeout,
63
64 install(): void {
65 globalThis.setTimeout = mockSetTimeout
66 globalThis.clearTimeout = mockClearTimeout
67 },
68
69 restore(): void {
70 globalThis.setTimeout = originalSetTimeout
71 globalThis.clearTimeout = originalClearTimeout
72 pendingTimers.length = 0
73 nextId = 1
74 currentTime = 0
75 },
76
77 runAll(): void {
78 const active = getActivePending()
79 for (const timer of active) {
80 if (timer.active) {
81 timer.active = false
82 timer.fn()
83 }
84 }
85 },

Callers 3

clipboard.test.tsFile · 0.90
installMockTimersFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected