From c79b3f1f9a591c48935b0beda5aa924ea392f02f Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 4 Jun 2024 12:40:45 +0200 Subject: [PATCH] fix(browser): specify entries for correct deps optimization (#5839) --- packages/browser/src/node/index.ts | 17 ++++++- .../vitest/src/integrations/browser/server.ts | 5 +- packages/vitest/src/node/index.ts | 3 ++ packages/vitest/src/node/pool.ts | 46 +++++++++---------- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index 0c036809b601..105c9ba1b416 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -6,8 +6,9 @@ import sirv from 'sirv' import type { ViteDevServer } from 'vite' import type { ResolvedConfig } from 'vitest' import type { BrowserScript, WorkspaceProject } from 'vitest/node' +import { getFilePoolName, distDir as vitestDist } from 'vitest/node' import { type Plugin, coverageConfigDefaults } from 'vitest/config' -import { slash } from '@vitest/utils' +import { slash, toArray } from '@vitest/utils' import BrowserContext from './plugins/pluginContext' import BrowserMocker from './plugins/pluginMocker' import DynamicImport from './plugins/pluginDynamicImport' @@ -155,8 +156,22 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => { name: 'vitest:browser:tests', enforce: 'pre', async config() { + const allTestFiles = await project.globTestFiles() + const browserTestFiles = allTestFiles.filter(file => getFilePoolName(project, file) === 'browser') + const setupFiles = toArray(project.config.setupFiles) + const vitestPaths = [ + resolve(vitestDist, 'index.js'), + resolve(vitestDist, 'browser.js'), + resolve(vitestDist, 'runners.js'), + resolve(vitestDist, 'utils.js'), + ] return { optimizeDeps: { + entries: [ + ...browserTestFiles, + ...setupFiles, + ...vitestPaths, + ], exclude: [ 'vitest', 'vitest/utils', diff --git a/packages/vitest/src/integrations/browser/server.ts b/packages/vitest/src/integrations/browser/server.ts index c53889ca75bf..ba9d3596ede4 100644 --- a/packages/vitest/src/integrations/browser/server.ts +++ b/packages/vitest/src/integrations/browser/server.ts @@ -24,6 +24,7 @@ export async function createBrowserServer(project: WorkspaceProject, configFile: server: { hmr: false, watch: null, + preTransformRequests: false, }, plugins: [ ...project.options?.plugins || [], @@ -58,10 +59,6 @@ export async function createBrowserServer(project: WorkspaceProject, configFile: resolve: { alias: config.test?.alias, }, - server: { - watch: null, - preTransformRequests: false, - }, } }, }, diff --git a/packages/vitest/src/node/index.ts b/packages/vitest/src/node/index.ts index 7781136ab7c8..752c0020e51a 100644 --- a/packages/vitest/src/node/index.ts +++ b/packages/vitest/src/node/index.ts @@ -8,8 +8,11 @@ export { registerConsoleShortcuts } from './stdin' export type { GlobalSetupContext } from './globalSetup' export type { WorkspaceSpec, ProcessPool } from './pool' export { createMethodsRPC } from './pools/rpc' +export { getFilePoolName } from './pool' export { VitestPackageInstaller } from './packageInstaller' +export { distDir, rootDir } from '../paths' + export type { TestSequencer, TestSequencerConstructor } from './sequencers/types' export { BaseSequencer } from './sequencers/BaseSequencer' diff --git a/packages/vitest/src/node/pool.ts b/packages/vitest/src/node/pool.ts index 8be517b80fe0..99d32b2acfa2 100644 --- a/packages/vitest/src/node/pool.ts +++ b/packages/vitest/src/node/pool.ts @@ -27,6 +27,28 @@ export interface PoolProcessOptions { export const builtinPools: BuiltinPool[] = ['forks', 'threads', 'browser', 'vmThreads', 'vmForks', 'typescript'] +function getDefaultPoolName(project: WorkspaceProject, file: string): Pool { + if (project.config.typecheck.enabled) { + for (const glob of project.config.typecheck.include) { + if (mm.isMatch(file, glob, { cwd: project.config.root })) + return 'typescript' + } + } + if (project.config.browser.enabled) + return 'browser' + return project.config.pool +} + +export function getFilePoolName(project: WorkspaceProject, file: string) { + for (const [glob, pool] of project.config.poolMatchGlobs) { + if ((pool as Pool) === 'browser') + throw new Error('Since Vitest 0.31.0 "browser" pool is not supported in "poolMatchGlobs". You can create a workspace to run some of your tests in browser in parallel. Read more: https://vitest.dev/guide/workspace') + if (mm.isMatch(file, glob, { cwd: project.config.root })) + return pool as Pool + } + return getDefaultPoolName(project, file) +} + export function createPool(ctx: Vitest): ProcessPool { const pools: Record = { forks: null, @@ -37,28 +59,6 @@ export function createPool(ctx: Vitest): ProcessPool { typescript: null, } - function getDefaultPoolName(project: WorkspaceProject, file: string): Pool { - if (project.config.typecheck.enabled) { - for (const glob of project.config.typecheck.include) { - if (mm.isMatch(file, glob, { cwd: project.config.root })) - return 'typescript' - } - } - if (project.config.browser.enabled) - return 'browser' - return project.config.pool - } - - function getPoolName([project, file]: WorkspaceSpec) { - for (const [glob, pool] of project.config.poolMatchGlobs) { - if ((pool as Pool) === 'browser') - throw new Error('Since Vitest 0.31.0 "browser" pool is not supported in "poolMatchGlobs". You can create a workspace to run some of your tests in browser in parallel. Read more: https://vitest.dev/guide/workspace') - if (mm.isMatch(file, glob, { cwd: project.config.root })) - return pool as Pool - } - return getDefaultPoolName(project, file) - } - // in addition to resolve.conditions Vite also adds production/development, // see: https://github.com/vitejs/vite/blob/af2aa09575229462635b7cbb6d248ca853057ba2/packages/vite/src/node/plugins/resolve.ts#L1056-L1080 const potentialConditions = new Set(['production', 'development', ...ctx.server.config.resolve.conditions]) @@ -137,7 +137,7 @@ export function createPool(ctx: Vitest): ProcessPool { } for (const spec of files) { - const pool = getPoolName(spec) + const pool = getFilePoolName(spec[0], spec[1]) filesByPool[pool] ??= [] filesByPool[pool].push(spec) }