Skip to content

Fix vi.mock() path resolution in generated vitest tests#1982

Open
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/vitest-mock-path-resolution
Open

Fix vi.mock() path resolution in generated vitest tests#1982
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/vitest-mock-path-resolution

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Problem

Generated vitest tests fail with module not found errors:

Error: Cannot find module '/routing/session-key.js' imported from
/workspace/target/test/test_filterBootstrapFilesForSession__unit_test_0.test.ts

Root Cause

The existing fix_jest_mock_paths() function only fixed jest.mock() calls but not vi.mock() calls.

When source code at src/agents/workspace.ts imports:

import { fn } from "../routing/session-key.js";

The generated test at test/test_workspace.test.ts had:

vi.mock('../routing/session-key', ...);  // ← Wrong! Resolves to /routing/session-key

Should be:

vi.mock('../src/routing/session-key', ...);  // ← Correct! Resolves to src/routing/session-key

The regex pattern only matched jest.mock() and jest.doMock(), missing vi.mock().

Solution

  1. Extended regex pattern to match vi.mock() in addition to jest patterns
  2. Updated function docstring to reflect support for both frameworks
  3. Updated debug logging to be framework-agnostic

Before:

mock_pattern = re.compile(r"(jest\.(?:mock|doMock)\s*\(\s*['\"])...")

After:

mock_pattern = re.compile(r"((?:jest|vi)\.(?:mock|doMock)\s*\(\s*['\"])...")

Testing

Added comprehensive unit tests:

  • test_fix_vitest_mock_paths() - Verifies vi.mock() paths are corrected
  • test_fix_jest_mock_paths_still_works() - Ensures jest.mock() still works

Both tests pass:

tests/test_fix_mock_paths_vitest.py::test_fix_vitest_mock_paths PASSED
tests/test_fix_mock_paths_vitest.py::test_fix_jest_mock_paths_still_works PASSED

Affected Trace

  • 265059d4-f518-44da-8367-d90ca424092c

🤖 Co-Authored-By: Claude Sonnet 4.5

Extended fix_jest_mock_paths() to handle vitest mock calls (vi.mock()) in
addition to jest.mock(). Previously, only jest.mock() paths were corrected,
causing vitest tests to fail with "Cannot find module" errors.

Problem:
- Source at src/agents/workspace.ts imports ../routing/session-key
- Generated test at test/test_workspace.test.ts used vi.mock('../routing/session-key')
- This resolves to /routing/session-key (wrong - goes up from test/, not found)
- Should be vi.mock('../src/routing/session-key') (correct path from test/)

Solution:
- Updated regex pattern to match both jest.mock() and vi.mock()
- Function now fixes relative paths for both test frameworks
- Added unit tests to verify both jest and vitest paths are corrected

Trace ID: 265059d4-f518-44da-8367-d90ca424092c

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown
Contributor

claude bot commented Apr 3, 2026

Claude finished @mohammedahmed18's task in 2m 12s —— View job


PR Review Summary

Triage

SMALL — 7 lines of production code changed in instrument.py, plus 94 lines of new tests.

Prek Checks

Passed. No issues.

Code Review

1. Function name not updated (instrument.py:1290)

The function is still named fix_jest_mock_paths despite now handling both Jest and Vitest. This is a misleading name — it should be renamed to fix_mock_paths (updating callers in support.py:2041 and support.py:2057 as well, and updating the existing tests in test_javascript_instrumentation.py). Fix this →

2. Weak/redundant assertion in new tests (tests/test_fix_mock_paths_vitest.py:52-53)

assert "../src/routing/session-key" in fixed    # ← correct
assert "../routing/session-key" not in fixed or "../src/routing/session-key" in fixed  # ← always true if line above passes

The second assertion is always satisfied when the first passes because "../routing/session-key" is a substring of "../src/routing/session-key". The intent was probably to assert the old path is gone, but substring matching makes it vacuous. Should be assert "vi.mock('../routing/session-key'" not in fixed (note the trailing quote to avoid the substring collision).

3. Tests placed in wrong file

New tests added to tests/test_fix_mock_paths_vitest.py instead of being co-located with the existing fix_jest_mock_paths tests in tests/test_languages/test_javascript_instrumentation.py. Splitting tests for the same function across files hurts discoverability.

Duplicate Detection

No duplicates detected. The regex change is surgical and the existing fix_jest_mock_paths tests in test_javascript_instrumentation.py cover the Jest path; the new tests cover the Vitest path.

Core Change

The regex fix itself ((?:jest|vi)\.(?:mock|doMock)) is correct and minimal — it also correctly handles vi.doMock() by design, which is good.


Last updated: 2026-04-03

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