Skip to content

test(watcher): fix fs.watch flake by adding settle delay#109

Open
andreinknv wants to merge 1 commit into
colbymchenry:mainfrom
andreinknv:fix/watcher-test-flake
Open

test(watcher): fix fs.watch flake by adding settle delay#109
andreinknv wants to merge 1 commit into
colbymchenry:mainfrom
andreinknv:fix/watcher-test-flake

Conversation

@andreinknv
Copy link
Copy Markdown
Contributor

Summary

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 tests

  • debounced sync > should trigger sync after file change — most frequently affected
  • 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

What changed

The filtering tests 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:

  • Factors that out into a single letWatcherSettle() helper with a docstring explaining the FSEvents/inotify latency.
  • 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

🤖 Generated with Claude Code

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant