test(db): load cold module graphs outside the test timeout #2276
main fromrefs/pull/2276/head·opened 🎯 Changes#
Fixes #2238.
Two @tanstack/db tests imported the Collection module graph inside the test body, so the cold transform and load counted against the 5 s default timeout. Under CPU load the import alone could use the whole budget.
This moves each import to file scope (top-level await import), after the setup the test depends on:
mutation-id.test.ts: stubcrypto,vi.resetModules(), then import. The globals are unstubbed inafterAll. The test still asserts that module evaluation drew no random value.collection-idle-cleanup.test.ts: install the controllable idle scheduler, then import. The binding check that requires per-file module isolation is unchanged.
The import now runs during collection, which has no test timeout. Neither test raises its timeout.
🔬 Oracle Coverage#
Test-only change. It changes no observable behavior and no oracle. Neither file is an oracle owner.
- RED (parent
db476b8ed):npx vitest run tests/mutation-id.test.ts tests/collection-idle-cleanup.test.ts --testTimeout=300fails withTest timed out in 300msin both tests. Without load, the in-body import takes about 500–630 ms of each test body. - GREEN: the same command passes. The test bodies take 5 ms and 84 ms; the 84 ms is the test's own 80 ms wait. Both tests also pass with the default timeout, and with 64 busy
yesprocesses on a 16-core machine (test bodies 92 ms total). - Mutants: each still fails the tests after the move.
- Drawing the mutation-id prefix at module scope (
let mutationIdPrefix = safeRandomUUID()) failsmutation-id: the spy was called once. - Removing
safeCancelIdleCallbackfromcancelGCTimerfailscollection-idle-cleanup: the status iscleaned-up, expectedready. - Loading
browser-polyfillsbefore the scheduler is installed fails the isolation check.
- Drawing the mutation-id prefix at module scope (
- Limits: I could not reproduce the original 5 s timeout locally. Load from
yesprocesses slowed the old test bodies only to about 1.9 s. The RED above shows the cause (the import is charged to the test budget), not the exact CI symptom.
✅ Checklist#
- I have tested this code locally with
pnpm test.@tanstack/db: 15477 passed, 1 failed.- The failure is
local-storage-peer-oracle.test.ts› "publishes through the default same-tab Storage object" (Cannot read properties of undefined (reading 'removeItem')). It fails the same way on unchangedmain. I ran it on Node 26.12, but the repo pins 24.8, so it is most likely an environment difference and is unrelated to this change.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Tests
- Added validation that collection cleanup remains correctly connected to idle scheduling.
- Expanded mutation ID checks to confirm inserts receive distinct IDs with a consistent runtime prefix, and that loading collection modules does not generate IDs.
- These checks help detect regressions in background cleanup and mutation identification.
- 1
No actionable comments were generated in the recent review.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: defaults
- Review profile: CHILL
- Plan: Advanced
- Run ID:
1f7d668f-6636-465f-831a-9ad7ac5c87f9
Commits-
Reviewing files that changed from the base of the PR and between db476b8ed9755e0c0b10b833d187354b4c63585c and 5e864c9374e8dfc779eaaef45502f58e7d3f465e.
Files selected for processing: 2-
packages/db/tests/collection-idle-cleanup.test.tspackages/db/tests/mutation-id.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
-
Walkthrough
-
The two database tests now load their collection module dependencies before their test bodies run. The mutation ID test retains checks for module-evaluation randomness and distinct prefixed IDs. The idle-cleanup test retains its scheduler and cleanup assertions.
Changes: Test import timing
Priority:Layer / File(s) Summary Preload collection modules
packages/db/tests/collection-idle-cleanup.test.ts,packages/db/tests/mutation-id.test.tsBoth tests move collection module imports to file scope. The mutation ID test restores its crypto.randomUUIDstub inafterAlland retains its ID assertions. The idle-cleanup test loads modules after installing the idle scheduler and retains its cleanup assertions. NormalEstimated code review effort: 2 (Simple) | ~10 minutes
Change: Other · Severity of issue fixed: Medium
Suggested reviewers:
kyleamathews
Merge Risk:
Minimal · up to5e864-
The tests preload dependencies outside their timed bodies while preserving setup and assertions. No merge-blocking risk was identified.
Pre-merge checks:
5-
Check name Status Explanation Title check The title clearly and concisely describes the main change: moving cold module-graph imports outside the test timeout. Description check The description covers the changes, motivation, oracle evidence, test results, known limitation, checklist, and release impact. It is complete and relevant to the template. Linked Issues check Issue #2238requires the two named tests to avoid charging cold module imports to the test timeout. The PR moves both imports to file scope.mutation-id.test.tsinstalls the crypto stub and calls `v…Out of Scope Changes check The diff changes only packages/db/tests/mutation-id.test.tsandpackages/db/tests/collection-idle-cleanup.test.ts. The changes move test setup and imports, preserve the test assertions, and restor…Docstring Coverage No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Full details: Linked Issues check
-
Explanation
Issue
#2238requires the two named tests to avoid charging cold module imports to the test timeout. The PR moves both imports to file scope.mutation-id.test.tsinstalls the crypto stub and callsvi.resetModules()before import.collection-idle-cleanup.test.tsinstalls the controllable idle scheduler before importingbrowser-polyfills.js. The existing assertions and timeouts remain. These changes satisfy the linked coding requirements.
Full details: Out of Scope Changes check
-
Explanation
The diff changes only
packages/db/tests/mutation-id.test.tsandpackages/db/tests/collection-idle-cleanup.test.ts. The changes move test setup and imports, preserve the test assertions, and restore test globals. They directly support issue#2238and do not alter published code or unrelated behavior.
Full details: Docstring Coverage
-
Explanation
No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands.-
Follow-up evidence: this closes the Limits gap above. Both tests now reproduce the original 5 s timeout locally and deterministically, and this PR fixes it under the same conditions.
Method. I ran the tests on the efficiency cores only (
taskpolicy -c background, macOS, M3 Max). This stands in for a slow or contended runner. The parent copies of both files time how long their in-bodyimport()takes. Node 26.12.0, default 5000 ms timeout.In-body import time, parent
db476b8ed(performance cores, no load):collection-idle-cleanupmutation-idcoverage off ~0.4 s ~0.6 s coverage on (Istanbul) 2.1 s 3.5 s (74% of the 5 s budget) Inside the full suite, the same tests take 0.2–0.35 s. Another file in the same worker has usually transformed the graph already. The timeout needs a cold worker.
RED / GREEN (efficiency cores, 3 runs each):
cd packages/db taskpolicy -c background pnpm vitest --run --coverage.enabled=true \ tests/mutation-id.test.ts tests/collection-idle-cleanup.test.tsVersion Coverage Result parent db476b8edon 3/3 runs: both tests Test timed out in 5000msparent db476b8edoff 3/3 passed this PR 5e864c937on 3/3 passed this PR 5e864c937off 3/3 passed Coverage instrumentation makes the cold import about 6× slower. That pushes
mutation-idclose to the 5 s budget even without load, which is consistent with #2238 appearing under CPU load. Load from two concurrent full-suite runs (8 workers on 16 cores) alone did not reproduce it: 6/6 runs passed, with imports at 2.2 s and 3.7 s.Limits: One machine, and efficiency cores stand in for a slow runner. I didn't rerun the mutants listed above on the efficiency cores.
jp-knj(GitHub profile, opens in a new tab) linked this to fix #2238
Sign in to join this conversation.
⚠️ No Changeset found#
Latest commit: 5e864c9374e8dfc779eaaef45502f58e7d3f465e
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR