Skip to content

test: import mocked-module graphs one at a time after vi.mock - #34103

Merged
nwparker merged 1 commit into
mainfrom
nwparker/vitest-mock-import-race-sweep
Oct 26, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/vitest-mock-import-race-sweep

Conversation

@nwparker

@nwparker nwparker commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​3 $\color{#cf222e}{\Huge{\mathbf{−}}}$​4 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​4 $\color{#cf222e}{\Huge{\mathbf{−}}}$​5 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1

ELI5

Some test setup code loaded several modules at the same time right after registering mocks. Vitest can let those loads go ahead before the mocks are in place, so a test can quietly use the real module instead of the mock. This makes the two remaining places load modules one at a time. It follows up #34085, which fixed the same problem in the daemon-init tests.

What Changed

  • src/main/jira/__mocks__/jira-client-module.ts (used by client.test.ts and client-scoped-token.test.ts): calls vi.doMock('os', …) and then used to Promise.all three dynamic imports. Now they're imported one after another, so the os home-dir mock is registered before ../client and its dependencies load.
  • src/renderer/src/lib/pane-manager/pane-tree-reparent-frame.test.ts: one test loaded pane-tree-ops and the mocked pane-divider together. It only raced when that test ran first (for example with -t). Now they're imported one at a time.

Each site has a one-line comment giving the reason. No assertions changed.

Why

Mechanism (Vitest 5.0.5). The first dynamic import calls resolveMocks(), which takes the queued mock ids and clears the queue before it awaits their resolution. A module fetch only waits for mocks while that queue is non-empty. So when several imports start at once, the later ones can load dependencies unmocked.

Fix. A single first import waits for every mock to register, so loading one at a time closes the race. The modules are still evaluated once per reset and share one module graph, so behavior is unchanged.

Sweep. I checked every Promise.all([... import(...)]) in src, tests and config. The others aren't affected: either the file has no pending mocks, or a static import has already flushed the queue, or the imports run inside a mock factory after registration (session-scanner-opencode-sqlite-coexistence.test.ts, language-detection-reconciliation.test.tsx, the cross-version wire tests).

Linked Issue

N/A (follow-up to #34085).

Visual Proof

N/A: test-only change.

Testing

  • client.test.ts, client-scoped-token.test.ts and pane-tree-reparent-frame.test.ts pass (50 tests). The reparent test also passes when run on its own with -t.

  • pnpm tc:node, pnpm tc:web, oxlint, the ast-grep gates and the changed-code gate pass.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Test-only. There's no runtime, SSH, mobile or wire impact. Until #34085 merges, this PR's unit shard 4 may still hit the daemon-init failure.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 787e3b1b-041c-49aa-89f0-a17225f09960
Commits

Reviewing files that changed from the base of the PR and between 55b2d9e and 2cb04e2.

Files selected for processing: 2
  • src/main/jira/__mocks__/jira-client-module.ts
  • src/renderer/src/lib/pane-manager/pane-tree-reparent-frame.test.ts

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: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f93e3b02-bd05-4eab-833d-104091be0b8b
Commits

Reviewing files that changed from the base of the PR and between a93e31a and 55b2d9e.

Files selected for processing: 2
  • src/main/jira/__mocks__/jira-client-module.ts
  • src/renderer/src/lib/pane-manager/pane-tree-reparent-frame.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Walkthrough

The Jira mock now imports the Jira client, request queue, and authenticated-request modules sequentially. The pane-manager test now imports pane-tree-ops and pane-divider sequentially. The test’s drag-active callback setup and assertion remain unchanged.

Priority: Low

Merge Risk: Minimal · previous review 55b2d

The changes are limited to test setup and do not introduce a merge-blocking production risk.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly states the main change: sequentially importing mocked module graphs after calling vi.mock.
Description check Passed The description is mostly complete. It explains the race condition, changed files, rationale, testing, scope, and checklist status. The Linked Issue section contains N/A instead of an actual issue lin…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is mostly complete. It explains the race condition, changed files, rationale, testing, scope, and checklist status. The Linked Issue section contains N/A instead of an actual issue link, although it references follow-up issue #34085.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Same Vitest registration race as the daemon-init fix: concurrent first imports
can load dependencies before pending vi.mock/vi.doMock calls are registered.
@nwparker
nwparker force-pushed the nwparker/vitest-mock-import-race-sweep branch from 55b2d9e to 2cb04e2 Compare October 26, 2026 22:07
@nwparker
nwparker merged commit b58513a into main Oct 26, 2026
33 checks passed
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