Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow inline workspace config #5246

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/vitest/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export function defineProject<T extends UserProjectConfigExport>(config: T): T {
return config
}

type Workspace = (string | (UserProjectConfigExport & { extends?: string }))
export type UserConfigWorkspaceDefinition = (string | (UserProjectConfigExport & { extends?: string }))

export function defineWorkspace(config: Workspace[]): Workspace[] {
export function defineWorkspace(config: UserConfigWorkspaceDefinition[]): UserConfigWorkspaceDefinition[] {
return config
}
2 changes: 1 addition & 1 deletion packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export function resolveConfig(
}
}

if (resolved.workspace) {
if (typeof resolved.workspace === 'string' && typeof options.workspace === 'string') {
// if passed down from the CLI and it's relative, resolve relative to CWD
resolved.workspace = options.workspace && options.workspace[0] === '.'
? resolve(process.cwd(), options.workspace)
Expand Down
15 changes: 10 additions & 5 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ export class Vitest {
if (!workspaceConfigPath)
return [await this.createCoreProject()]

const workspaceModule = await this.runner.executeFile(workspaceConfigPath) as {
default: ReturnType<typeof defineWorkspace>
}
const workspaceModule = typeof workspaceConfigPath === 'string'
? await this.runner.executeFile(workspaceConfigPath) as {
default: ReturnType<typeof defineWorkspace>
}
: { default: workspaceConfigPath }

if (!workspaceModule.default || !Array.isArray(workspaceModule.default))
throw new Error(`Workspace config file ${workspaceConfigPath} must export a default array of project paths.`)
Expand Down Expand Up @@ -309,17 +311,20 @@ export class Vitest {
return acc
}, {} as UserConfig)

const configPath = typeof workspaceConfigPath === 'string'
? workspaceConfigPath
: (this.server.config.configFile || this.config.root)
const projects = filteredWorkspaces.map(async (workspacePath) => {
// don't start a new server, but reuse existing one
if (
this.server.config.configFile === workspacePath
)
return this.createCoreProject()
return initializeProject(workspacePath, this, { workspaceConfigPath, test: cliOverrides })
return initializeProject(workspacePath, this, { workspaceConfigPath: configPath, test: cliOverrides })
})

projectsOptions.forEach((options, index) => {
projects.push(initializeProject(index, this, mergeConfig(options, { workspaceConfigPath, test: cliOverrides }) as any))
projects.push(initializeProject(index, this, mergeConfig(options, { workspaceConfigPath: configPath, test: cliOverrides }) as any))
})

if (!projects.length)
Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ViteNodeServerOptions } from 'vite-node'
import type { BuiltinReporterOptions, BuiltinReporters } from '../node/reporters'
import type { TestSequencerConstructor } from '../node/sequencers/types'
import type { ChaiConfig } from '../integrations/chai/config'
import type { UserConfigWorkspaceDefinition } from '../config'
import type { CoverageOptions, ResolvedCoverageOptions } from './coverage'
import type { JSDOMOptions } from './jsdom-options'
import type { HappyDOMOptions } from './happy-dom-options'
Expand Down Expand Up @@ -345,9 +346,9 @@ export interface InlineConfig {
poolMatchGlobs?: [string, Exclude<Pool, 'browser'>][]

/**
* Path to a workspace configuration file
* Workspace configuration or a path to a workspace configuration file
*/
workspace?: string
workspace?: string | UserConfigWorkspaceDefinition[]
AriPerkkio marked this conversation as resolved.
Show resolved Hide resolved

/**
* Update snapshot
Expand Down