From 0bf52533284195eb2ef9655320c4330cd15cb147 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 21:12:28 +0900 Subject: [PATCH] fix(vitest): handle function config inside `defineWorkspace` (#5089) --- packages/vitest/src/node/core.ts | 19 +++++++++++++++---- test/workspaces/vitest.workspace.ts | 8 ++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index ed1508c112b5..1d048e5662e0 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -9,6 +9,7 @@ import { ViteNodeRunner } from 'vite-node/client' import { SnapshotManager } from '@vitest/snapshot/manager' import type { CancelReason, File } from '@vitest/runner' import { ViteNodeServer } from 'vite-node/server' +import type { defineWorkspace } from 'vitest/config' import type { ArgumentsType, CoverageProvider, OnServerRestartHandler, Reporter, ResolvedConfig, UserConfig, UserWorkspaceConfig, VitestRunMode } from '../types' import { hasFailed, noop, slash, toArray } from '../utils' import { getCoverageProvider } from '../integrations/coverage' @@ -215,7 +216,7 @@ export class Vitest { return [await this.createCoreProject()] const workspaceModule = await this.runner.executeFile(workspaceConfigPath) as { - default: (string | UserWorkspaceConfig)[] + default: ReturnType } if (!workspaceModule.default || !Array.isArray(workspaceModule.default)) @@ -225,10 +226,20 @@ export class Vitest { const projectsOptions: UserWorkspaceConfig[] = [] for (const project of workspaceModule.default) { - if (typeof project === 'string') + if (typeof project === 'string') { workspaceGlobMatches.push(project.replace('', this.config.root)) - else - projectsOptions.push(project) + } + else if (typeof project === 'function') { + projectsOptions.push(await project({ + command: this.server.config.command, + mode: this.server.config.mode, + isPreview: false, + isSsrBuild: false, + })) + } + else { + projectsOptions.push(await project) + } } const globOptions: fg.Options = { diff --git a/test/workspaces/vitest.workspace.ts b/test/workspaces/vitest.workspace.ts index c8481a055402..038fed32aa47 100644 --- a/test/workspaces/vitest.workspace.ts +++ b/test/workspaces/vitest.workspace.ts @@ -6,22 +6,22 @@ import type { Plugin } from 'vite' export default defineWorkspace([ 'space_2', './space_*/*.config.ts', - { + async () => ({ test: { name: 'happy-dom', root: './space_shared', environment: 'happy-dom', setupFiles: ['./setup.jsdom.ts'], }, - }, - { + }), + Promise.resolve({ test: { name: 'node', root: './space_shared', environment: 'node', setupFiles: ['./setup.node.ts'], }, - }, + }), // Projects testing pool and poolOptions {