Skip to content

Commit 73d7a77

Browse files
authored
Merge pull request #1986 from codeflash-ai/fix/vitest-setupfiles-path-resolution
Fix Vitest setupFiles path resolution and workspace detection
2 parents 8d51e2d + 9d22f43 commit 73d7a77

2 files changed

Lines changed: 85 additions & 3 deletions

File tree

codeflash/languages/javascript/vitest_runner.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import os
10+
import re
1011
import subprocess
1112
import time
1213
from pathlib import Path
@@ -169,9 +170,24 @@ def _is_vitest_workspace(project_root: Path) -> bool:
169170
return False
170171

171172
try:
172-
content = vitest_config.read_text()
173-
# Check for workspace indicators
174-
return "workspace" in content.lower() or "defineWorkspace" in content
173+
content = vitest_config.read_text(encoding="utf-8")
174+
# Check for actual workspace configuration patterns (not just the word "workspace" in comments)
175+
# Valid indicators:
176+
# - defineWorkspace() function call
177+
# - workspace: [ array config
178+
# - separate vitest.workspace.ts/js file
179+
# Match defineWorkspace calls or workspace: property assignments
180+
workspace_pattern = re.compile(
181+
r"(?:^|[^a-zA-Z_])defineWorkspace\s*\(|" # defineWorkspace( function call
182+
r"(?:^|[^a-zA-Z_])workspace\s*:\s*\[", # workspace: [ array
183+
re.MULTILINE,
184+
)
185+
if workspace_pattern.search(content):
186+
return True
187+
# Also check for separate workspace config file
188+
if (project_root / "vitest.workspace.ts").exists() or (project_root / "vitest.workspace.js").exists():
189+
return True
190+
return False
175191
except Exception:
176192
return False
177193

@@ -238,6 +254,11 @@ def _ensure_codeflash_vitest_config(project_root: Path) -> Path | None:
238254
include: ['**/*.test.ts', '**/*.test.js', '**/*.test.tsx', '**/*.test.jsx'],
239255
// Use forks pool so timing markers from process.stdout.write flow to parent stdout
240256
pool: 'forks',
257+
// Disable setupFiles to prevent relative path resolution issues in nested directories.
258+
// Project setupFiles often use relative paths (e.g., "test/setup.ts") which resolve
259+
// incorrectly when tests are in subdirectories (e.g., extensions/discord/test/).
260+
// Codeflash-generated tests are self-contained and don't require project setup files.
261+
setupFiles: [],
241262
}},
242263
}});
243264
"""
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from codeflash.languages.javascript.vitest_runner import _ensure_codeflash_vitest_config
6+
7+
8+
def test_codeflash_vitest_config_overrides_setupfiles(tmp_path: Path) -> None:
9+
project_root = tmp_path.resolve()
10+
11+
# Create a project with setup file
12+
(project_root / "test").mkdir()
13+
(project_root / "test" / "setup.ts").write_text("// Setup file\n", encoding="utf-8")
14+
15+
vitest_config = """import { defineConfig } from 'vitest/config';
16+
17+
export default defineConfig({
18+
test: {
19+
setupFiles: ["test/setup.ts"], // Relative path - will cause issues
20+
include: ["src/**/*.test.ts"],
21+
},
22+
});
23+
"""
24+
(project_root / "vitest.config.ts").write_text(vitest_config, encoding="utf-8")
25+
26+
codeflash_config_path = _ensure_codeflash_vitest_config(project_root)
27+
28+
assert codeflash_config_path is not None
29+
assert codeflash_config_path.exists()
30+
31+
config_content = codeflash_config_path.read_text(encoding="utf-8")
32+
33+
assert "setupFiles" in config_content, (
34+
"Generated config must explicitly handle setupFiles to prevent "
35+
"relative path resolution issues. Current config:\n" + config_content
36+
)
37+
assert "setupFiles: []" in config_content or "setupFiles:" in config_content, (
38+
"setupFiles must be explicitly set in the merged config"
39+
)
40+
41+
42+
def test_codeflash_vitest_config_without_setupfiles(tmp_path: Path) -> None:
43+
project_root = tmp_path.resolve()
44+
45+
vitest_config = """import { defineConfig } from 'vitest/config';
46+
47+
export default defineConfig({
48+
test: {
49+
include: ["src/**/*.test.ts"],
50+
},
51+
});
52+
"""
53+
(project_root / "vitest.config.ts").write_text(vitest_config, encoding="utf-8")
54+
55+
codeflash_config_path = _ensure_codeflash_vitest_config(project_root)
56+
57+
assert codeflash_config_path is not None
58+
assert codeflash_config_path.exists()
59+
60+
config_content = codeflash_config_path.read_text(encoding="utf-8")
61+
assert "mergeConfig" in config_content or "defineConfig" in config_content

0 commit comments

Comments
 (0)