* Discovers all vitest projects from package configs. * For configs with nested projects, expands them with the correct root path. * For simple configs, returns the directory as a project path.
()
| 35 | * For simple configs, returns the directory as a project path. |
| 36 | */ |
| 37 | async function discoverProjects(): Promise<TestProjectConfiguration[]> { |
| 38 | const projects: TestProjectConfiguration[] = []; |
| 39 | |
| 40 | // Find all vitest.config.ts files |
| 41 | const configPaths = PROJECT_GLOBS.flatMap(pattern => globSync(pattern)); |
| 42 | |
| 43 | for (const configPath of configPaths) { |
| 44 | const projectDir = dirname(configPath); |
| 45 | |
| 46 | // Skip excluded directories |
| 47 | if (EXCLUDED_DIRS.has(projectDir)) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | // Read the config file to check if it has nested projects |
| 52 | const configContent = readFileSync(configPath, 'utf-8'); |
| 53 | const hasNestedProjects = /test:\s*\{[\s\S]*?projects:\s*\[/.test(configContent); |
| 54 | |
| 55 | if (!hasNestedProjects) { |
| 56 | // Simple config - use directory path |
| 57 | projects.push(projectDir); |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | // Config has nested projects - load it using Vite's config loader |
| 62 | try { |
| 63 | const absolutePath = resolve(process.cwd(), configPath); |
| 64 | const loaded = await loadConfigFromFile({} as any, absolutePath); |
| 65 | if (!loaded) { |
| 66 | projects.push(projectDir); |
| 67 | continue; |
| 68 | } |
| 69 | const config = loaded.config as UserWorkspaceConfig; |
| 70 | |
| 71 | if (!config.test?.projects) { |
| 72 | // Fallback if config parsing didn't work as expected |
| 73 | projects.push(projectDir); |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | // Expand nested projects with root path |
| 78 | for (const nestedProject of config.test.projects) { |
| 79 | if (typeof nestedProject === 'string') { |
| 80 | // String reference - resolve relative to the config's directory |
| 81 | projects.push(`${projectDir}/${nestedProject}`); |
| 82 | } else { |
| 83 | // Inline project config - add root path |
| 84 | const projectConfig = nestedProject as UserWorkspaceConfig; |
| 85 | projects.push({ |
| 86 | ...projectConfig, |
| 87 | test: { |
| 88 | ...projectConfig.test, |
| 89 | root: `./${projectDir}`, |
| 90 | }, |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | } catch (error) { |