Skip to content

Commit 81b67b2

Browse files
[test] replace arbitrary setTimeout with vi.waitFor for better test reliability - addresses review feedback
Replaces brittle setTimeout(50) with vitest's vi.waitFor() which polls for the expected condition. This eliminates flaky test failures on slower systems while maintaining fast execution when conditions are met quickly.
1 parent 25d1452 commit 81b67b2

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

tests-ui/tests/renderer/core/layout/layoutStore.test.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it } from 'vitest'
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
22

33
import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
44
import {
@@ -165,10 +165,11 @@ describe('layoutStore CRDT operations', () => {
165165
actor: layoutStore.getCurrentActor()
166166
})
167167

168-
// Wait for async notification
169-
await new Promise((resolve) => setTimeout(resolve, 50))
168+
// Wait for onChange callback to be called (uses setTimeout internally)
169+
await vi.waitFor(() => {
170+
expect(changes.length).toBeGreaterThanOrEqual(1)
171+
})
170172

171-
expect(changes.length).toBeGreaterThanOrEqual(1)
172173
const lastChange = changes[changes.length - 1]
173174
expect(lastChange.source).toBe('vue')
174175
expect(lastChange.operation.actor).toBe('user-123')
@@ -198,11 +199,14 @@ describe('layoutStore CRDT operations', () => {
198199
const newBounds = { x: 40, y: 60, width: 220, height: 120 }
199200
layoutStore.batchUpdateNodeBounds([{ nodeId, bounds: newBounds }])
200201

201-
await new Promise((resolve) => setTimeout(resolve, 50))
202+
// Wait for onChange callback to be called (uses setTimeout internally)
203+
await vi.waitFor(() => {
204+
expect(changes.length).toBeGreaterThan(0)
205+
const lastChange = changes[changes.length - 1]
206+
expect(lastChange.operation.type).toBe('batchUpdateBounds')
207+
})
202208

203-
expect(changes.length).toBeGreaterThan(0)
204209
const lastChange = changes[changes.length - 1]
205-
expect(lastChange.operation.type).toBe('batchUpdateBounds')
206210
if (lastChange.operation.type === 'batchUpdateBounds') {
207211
expect(lastChange.nodeIds).toContain(nodeId)
208212
expect(lastChange.operation.bounds[nodeId]?.bounds).toEqual(newBounds)

0 commit comments

Comments
 (0)