Fix flaky unit tests by replacing fixed sleeps with vi.waitFor#359
Merged
umair-ably merged 1 commit intomainfrom Apr 20, 2026
Merged
Conversation
These tests used `setTimeout` (either scheduling a callback to fire
during a `runCommand`, or a fixed `await new Promise(setTimeout, N)`)
to wait for async work to complete before asserting. When the work
took longer than the timeout the assertion ran against stale state
and the test failed.
Switched to `vi.waitFor(() => expect(...))` which polls until the
condition holds, removing the race:
- logs/connection-lifecycle/subscribe: 5 tests fired the message
callback at +50ms from test start, but the command's
`channel.subscribe()` was not always captured by then.
- interactive-terminal-behavior: two tests slept 100ms after
`rl.emit("line", ...)` then asserted on async prompt/close state.
- spaces/locations/subscribe: two tests slept 50ms after starting
the command before invoking the captured subscribe handler.
- hooks/interactive-did-you-mean: slept 30ms after the hook
returned before asserting on readline state restoration.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughFour unit test files used fixed Changes
Review Notes
|
AndyTWF
approved these changes
Apr 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Four unit test files used fixed
setTimeoutwaits to synchronise with async work insiderunCommand. When the work took longer than the hard-coded delay, the assertion ran against stale state and the test failed intermittently.Replaced those fixed sleeps with
vi.waitFor(() => expect(...)), which polls the condition and eliminates the race.Files changed:
test/unit/commands/logs/connection-lifecycle/subscribe.test.ts— 5 tests scheduledsetTimeout(..., 50)to fire the message callback, butchannel.subscribe()wasn't always captured within 50 ms, so the callback check sawnulland silently dropped the message. (This was the actual failure that triggered the investigation.)test/unit/commands/interactive-terminal-behavior.test.ts— 2 tests slept 100 ms afterrl.emit("line", ...)then asserted on async prompt/close state.test/unit/commands/spaces/locations/subscribe.test.ts— 2 tests slept 50 ms after starting the command before invoking the captured subscribe handler.test/unit/hooks/interactive-did-you-mean.test.ts— slept 30 ms after the hook returned before asserting on readline state restoration.Test plan
pnpm exec eslint .— 0 errors (7 pre-existing warnings)pnpm test:unit— 2292 passed, 0 failed