Skip to content

Commit 3b725dd

Browse files
committed
test(stx): avoid global clock in async suites
1 parent 9e8c687 commit 3b725dd

3 files changed

Lines changed: 24 additions & 29 deletions

File tree

packages/stx/test/performance/performance-utils.test.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -526,42 +526,33 @@ describe('Performance Utils', () => {
526526
expect(result2).toBe('DLROW')
527527
})
528528

529-
it('should handle throttling for high-frequency operations', () => {
529+
it('should handle throttling for high-frequency operations', async () => {
530530
let executionCount = 0
531531
const operation = () => {
532532
executionCount++
533533
return executionCount
534534
}
535535

536-
const originalNow = Date.now
537-
let now = 1_000
538-
Date.now = () => now
536+
const throttledOperation = throttle(operation, 200)
539537

540-
try {
541-
const throttledOperation = throttle(operation, 100)
542-
543-
// Rapid consecutive calls
544-
const results = []
545-
for (let i = 0; i < 10; i++) {
546-
results.push(throttledOperation())
547-
}
538+
// Rapid consecutive calls
539+
const results = []
540+
for (let i = 0; i < 10; i++) {
541+
results.push(throttledOperation())
542+
}
548543

549-
// Should execute immediately for first call
550-
expect(results[0]).toBe(1)
544+
// Should execute immediately for first call
545+
expect(results[0]).toBe(1)
551546

552-
// Subsequent calls should be throttled
553-
for (let i = 1; i < 10; i++) {
554-
expect(results[i]).toBe(1) // Same result as first call
555-
}
547+
// Subsequent calls should be throttled
548+
for (let i = 1; i < 10; i++) {
549+
expect(results[i]).toBe(1) // Same result as first call
550+
}
556551

557-
now += 100
552+
await new Promise(resolve => setTimeout(resolve, 300))
558553

559-
const nextResult = throttledOperation()
560-
expect(nextResult).toBe(2) // New execution
561-
}
562-
finally {
563-
Date.now = originalNow
564-
}
554+
const nextResult = throttledOperation()
555+
expect(nextResult).toBe(2) // New execution
565556
})
566557

567558
it('should handle debouncing for batching operations', async () => {

packages/stx/test/state-management/middleware.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@ describe('throttleMiddleware', () => {
9191
// First update should go through immediately
9292
expect(values).toEqual([1])
9393

94-
// Wait for throttle period
95-
await new Promise(resolve => setTimeout(resolve, 60))
94+
// Wait for the trailing throttled update instead of assuming exact timer cadence.
95+
const deadline = Date.now() + 500
96+
while (!values.includes(3) && Date.now() < deadline) {
97+
await new Promise(resolve => setTimeout(resolve, 10))
98+
}
9699

97100
// Last value should be applied
98101
expect(values).toContain(3)

packages/stx/test/state-management/signals-integration.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ describe('signals integration - async patterns', () => {
375375
const userId = state<number | null>(null)
376376
const userData = state<{ name: string } | null>(null)
377377
const loading = state(false)
378+
let fetchStarted = false
378379
let resolveFetched!: () => void
379380
const fetched = new Promise<void>((resolve) => {
380381
resolveFetched = resolve
@@ -384,6 +385,7 @@ describe('signals integration - async patterns', () => {
384385
effect(() => {
385386
const id = userId()
386387
if (id !== null) {
388+
fetchStarted = true
387389
loading.set(true)
388390
setTimeout(() => {
389391
userData.set({ name: `User ${id}` })
@@ -395,10 +397,9 @@ describe('signals integration - async patterns', () => {
395397

396398
userId.set(1)
397399

398-
expect(loading()).toBe(true)
399-
400400
await fetched
401401

402+
expect(fetchStarted).toBe(true)
402403
expect(loading()).toBe(false)
403404
expect(userData()?.name).toBe('User 1')
404405
})

0 commit comments

Comments
 (0)