From ed9b805c2b8735e12206e4bd02a67946fe829566 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 11 Apr 2023 10:10:42 +0200 Subject: [PATCH] fix: allow workspace without a config in the root --- packages/vitest/src/node/core.ts | 26 ++++++++++++++------------ packages/vitest/src/node/workspace.ts | 12 ++++++++---- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index add6e417f39e..9383ee21eea3 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -152,7 +152,9 @@ export class Vitest { } private async resolveWorkspace(options: UserConfig, cliOptions: UserConfig) { - const configDir = dirname(this.server.config.configFile || this.config.root) + const configDir = this.server.config.configFile + ? dirname(this.server.config.configFile) + : this.config.root const rootFiles = await fs.readdir(configDir) const workspaceConfigName = workspaceFiles.find((configFile) => { return rootFiles.includes(configFile) @@ -161,21 +163,21 @@ export class Vitest { if (!workspaceConfigName) return [await this.createCoreWorkspace(options)] - const workspacesConfigPath = join(configDir, workspaceConfigName) + const workspaceConfigPath = join(configDir, workspaceConfigName) - const workspacesModule = await this.runner.executeFile(workspacesConfigPath) as { + const workspaceModule = await this.runner.executeFile(workspaceConfigPath) as { default: (string | UserWorkspaceConfig)[] } - if (!workspacesModule.default || !Array.isArray(workspacesModule.default)) - throw new Error(`Workspace config file ${workspacesConfigPath} must export a default array of project paths.`) + if (!workspaceModule.default || !Array.isArray(workspaceModule.default)) + throw new Error(`Workspace config file ${workspaceConfigPath} must export a default array of project paths.`) - const workspacesGlobMatches: string[] = [] + const workspaceGlobMatches: string[] = [] const projectsOptions: UserWorkspaceConfig[] = [] - for (const project of workspacesModule.default) { + for (const project of workspaceModule.default) { if (typeof project === 'string') - workspacesGlobMatches.push(project.replace('', this.config.root)) + workspaceGlobMatches.push(project.replace('', this.config.root)) else projectsOptions.push(project) } @@ -189,7 +191,7 @@ export class Vitest { ignore: ['**/node_modules/**'], } - const workspacesFs = await fg(workspacesGlobMatches, globOptions) + const workspacesFs = await fg(workspaceGlobMatches, globOptions) const resolvedWorkspacesPaths = await Promise.all(workspacesFs.filter((file) => { if (file.endsWith('/')) { // if it's a directory, check that we don't already have a workspace with a config inside @@ -204,7 +206,7 @@ export class Vitest { if (filepath.endsWith('/')) { const filesInside = await fs.readdir(filepath) const configFile = configFiles.find(config => filesInside.includes(config)) - return configFile || filepath + return configFile ? join(filepath, configFile) : filepath } return filepath })) @@ -233,11 +235,11 @@ export class Vitest { this.server.config.configFile === workspacePath ) return this.createCoreWorkspace(options) - return initializeProject(workspacePath, this, { test: cliOverrides }) + return initializeProject(workspacePath, this, { workspaceConfigPath, test: cliOverrides }) }) projectsOptions.forEach((options, index) => { - projects.push(initializeProject(index, this, mergeConfig(options, { test: cliOverrides }))) + projects.push(initializeProject(index, this, mergeConfig(options, { workspaceConfigPath, test: cliOverrides }) as any)) }) if (!projects.length) diff --git a/packages/vitest/src/node/workspace.ts b/packages/vitest/src/node/workspace.ts index 895d4e79f280..996eeb57a7b7 100644 --- a/packages/vitest/src/node/workspace.ts +++ b/packages/vitest/src/node/workspace.ts @@ -14,16 +14,20 @@ import { isBrowserEnabled, resolveConfig } from './config' import { WorkspaceVitestPlugin } from './plugins/workspace' import { VitestServer } from './server' -interface InitializeOptions { +interface InitializeServerOptions { server?: VitestServer runner?: ViteNodeRunner } -export async function initializeProject(workspacePath: string | number, ctx: Vitest, options: UserWorkspaceConfig = {}) { +interface InitializeProjectOptions extends UserWorkspaceConfig { + workspaceConfigPath: string +} + +export async function initializeProject(workspacePath: string | number, ctx: Vitest, options: InitializeProjectOptions) { const project = new WorkspaceProject(workspacePath, ctx) const configFile = options.extends - ? resolve(ctx.config.root, options.extends) + ? resolve(dirname(options.workspaceConfigPath), options.extends) : (typeof workspacePath === 'number' || workspacePath.endsWith('/')) ? false : workspacePath @@ -143,7 +147,7 @@ export class WorkspaceProject { this.browser = await createBrowserServer(this, options) } - async setServer(options: UserConfig, server: ViteDevServer, params: InitializeOptions = {}) { + async setServer(options: UserConfig, server: ViteDevServer, params: InitializeServerOptions = {}) { this.config = resolveConfig(this.ctx.mode, options, server.config) this.server = server