test(watcher): fix fs.watch flake by adding settle delay#27
Open
mschreib28 wants to merge 1 commit into
Open
Conversation
…write
Three watcher tests wrote files immediately after `watcher.start()`,
hitting a race with macOS FSEvents (and Linux inotify): there's a small
but real latency between `fs.watch()` returning and the kernel actually
delivering events. Under parallel test load (when the host CPU is busy
running other test files in worker threads), that latency balloons and
the file-change event is dropped before the watcher is fully registered.
Result: the test waits 5s for a sync that never fires and times out.
Affected:
- debounced sync > should trigger sync after file change
- callbacks > should call onSyncComplete after successful sync
- callbacks > should call onSyncError when sync throws
- debounced sync > should debounce rapid changes (less affected; its
50ms-spaced loop incidentally settles, but explicit is better)
- CodeGraph integration > should auto-sync when files change
Other tests in the same file already had a 400ms settle delay with a
comment ("Let watcher settle — fs.watch may fire residual events from
beforeEach") in the filtering tests. This PR factors that out into a
`letWatcherSettle()` helper and applies it consistently to every test
that writes immediately after `start()`.
No production code change. The flake had no user impact — it was purely
a test-order artifact under parallel load.
## Verification
- Pre-fix: ran `npm test` 8+ times across this session — fs.watch flake
fired in roughly 1 of 3 runs under parallel load.
- Post-fix: 3 consecutive `npm test` runs, 380/380 each, no flakes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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\n\nThree watcher tests wrote files immediately after
watcher.start(), hitting a race with macOS FSEvents (and Linux inotify): there's a small but real latency betweenfs.watch()returning and the kernel actually delivering events. Under parallel test load (when the host CPU is busy running other test files in worker threads), that latency balloons and the file-change event is dropped before the watcher is fully registered. Result: the test waits 5s for a sync that never fires and times out.\n\n## Affected tests\n\n-debounced sync > should trigger sync after file change— most frequently affected\n-callbacks > should call onSyncComplete after successful sync\n-callbacks > should call onSyncError when sync throws\n-debounced sync > should debounce rapid changes— less affected (its 50ms-spaced loop incidentally settles, but explicit is better)\n-CodeGraph integration > should auto-sync when files change\n\n## What changed\n\nThefilteringtests in this file already had a 400ms settle delay with a comment ("Let watcher settle — fs.watch may fire residual events from beforeEach"). This PR:\n\n- Factors that out into a singleletWatcherSettle()helper with a docstring explaining the FSEvents/inotify latency.\n- Applies it consistently to every test that writes immediately afterstart().\n- No production code change — the flake had no user impact, it was purely a test-order artifact under parallel load.\n\n## Verification\n\n- Pre-fix: rannpm test8+ times across the session that produced PRs colbymchenry#100–colbymchenry#108. The fs.watch flake fired in roughly 1 of 3 runs under parallel load.\n- Post-fix: 3 consecutivenpm testruns, 380/380 each, zero flakes.\n\n🤖 Generated with Claude Code\nCopied from colbymchenry/codegraph#109