Skip to content

Commit

Permalink
fix: allow workspace without a config in the root (#3173)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 11, 2023
1 parent cce4549 commit 06852f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
26 changes: 14 additions & 12 deletions packages/vitest/src/node/core.ts
Expand Up @@ -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)
Expand All @@ -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('<rootDir>', this.config.root))
workspaceGlobMatches.push(project.replace('<rootDir>', this.config.root))
else
projectsOptions.push(project)
}
Expand All @@ -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
Expand All @@ -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
}))
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions packages/vitest/src/node/workspace.ts
Expand Up @@ -14,16 +14,21 @@ 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 & { extends?: string }) = {}) {
interface InitializeProjectOptions extends UserWorkspaceConfig {
workspaceConfigPath: string
extends?: 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
Expand Down Expand Up @@ -143,7 +148,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

Expand Down

0 comments on commit 06852f1

Please sign in to comment.