Fix false positive test discovery from substring matching#1974
Conversation
Issue: Test discovery incorrectly matched test files with source functions
when the function name appeared anywhere in the test file, including in
mocks, comments, or unrelated code. This caused 'Failed to instrument test
file' errors.
Root cause: In javascript/support.py line 259, naive substring matching
(func.function_name in source) matched function names even when they were
only mentioned in mocks like:
vi.mock('./file.js', () => ({ funcName: ... }))
Example: Function parseRestartRequestParams from restart-request.ts was
wrongly matched with update.test.ts because the test file mocked it.
Fix: Removed substring matching, now only matches explicitly imported
functions. This is more reliable and avoids false positives.
Trace ID: 0b575a96-62a8-4910-b163-1ad10e60ba79
Changes:
- Removed naive substring check in discover_tests()
- Only match functions that are explicitly imported
- Added regression tests (2 test cases)
Testing:
- All 70 JavaScript tests pass
- New tests verify fix works correctly
- Linting/type checks pass (uv run prek)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Codeflash Bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
|
Claude finished @mohammedahmed18's task in 3m 33s —— View job PR Review
Prek Checks
Code Review
source_function = FunctionToOptimize(
qualified_name="parseRestartRequestParams", # ❌ not a constructor param — it's a @property
function_name="parseRestartRequestParams",
file_path=test_root / "restart-request.ts",
starting_line=1,
ending_line=10,
function_signature="", # ❌ not a constructor param
code_position=CodePosition(line_no=1, col_no=0), # ❌ not a constructor param
file_path_relative_to_project_root="restart-request.ts", # ❌ not a constructor param
)Pydantic silently drops the unknown kwargs (no runtime error), but source_function = FunctionToOptimize(
function_name="parseRestartRequestParams",
file_path=test_root / "restart-request.ts",
)Duplicate DetectionNo duplicates detected. The Bot PR CheckOne open codeflash-ai[bot] PR found: #1954 ("⚡️ Speed up |
…se-positive-test-discovery
…se-positive-test-discovery
…S test discovery The index-only approach missed tests for class methods (imported by class name), aliased imports (only alias was tracked), and namespace imports (e.g. math.calculate). Adds class_name→methods index, tracks both original+alias names, and extracts namespace member access via regex. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Bug 4: False Positive Test Discovery
Trace ID: 0b575a96-62a8-4910-b163-1ad10e60ba79
Issue
Test discovery incorrectly matched test files with source functions when the function name appeared anywhere in the test file, including in mocks, comments, or unrelated code. This caused
Failed to instrument test fileerrors because Codeflash tried to instrument tests that don't actually test the target function.Root Cause
In
/opt/codeflash/codeflash/languages/javascript/support.pyline 259, the condition:Used naive substring matching (
func.function_name in source) which matched function names even when they were only mentioned in mocks:Example
parseRestartRequestParamsfromrestart-request.tsupdate.test.tsvi.mock("./restart-request.js", () => ({ parseRestartRequestParams: ... }))Fix
Removed the substring matching condition. Now only matches functions that are explicitly imported from the source module. This is more reliable and avoids false positives.
Changes
codeflash/languages/javascript/support.py(line 257-266)tests/languages/javascript/test_false_positive_discovery.py(2 regression tests)Testing
uv run prek)Impact