From 9bb941666218e439438cd9fb3222fd2cc9522287 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 3 Sep 2024 15:09:12 +0200 Subject: [PATCH 1/5] fix(browser): exclude missed packages from optimization, print help message --- packages/browser/src/node/plugin.ts | 48 +++++++++++++++-------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/browser/src/node/plugin.ts b/packages/browser/src/node/plugin.ts index 10c5f535418e..c9d59a540198 100644 --- a/packages/browser/src/node/plugin.ts +++ b/packages/browser/src/node/plugin.ts @@ -182,6 +182,29 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => { ...(project.config.snapshotSerializers || []), ] + const exclude = [ + 'vitest', + 'vitest/utils', + 'vitest/browser', + 'vitest/runners', + '@vitest/browser', + '@vitest/browser/client', + '@vitest/utils', + '@vitest/utils/source-map', + '@vitest/runner', + '@vitest/spy', + '@vitest/utils/error', + '@vitest/snapshot', + '@vitest/expect', + 'std-env', + 'tinybench', + 'tinyspy', + 'tinyrainbow', + 'pathe', + 'msw', + 'msw/browser', + ] + if (project.config.diff) { entries.push(project.config.diff) } @@ -193,12 +216,14 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => { const path = tryResolve('@vitest/coverage-v8', [project.ctx.config.root]) if (path) { entries.push(path) + exclude.push('@vitest/coverage-v8/browser') } } else if (provider === 'istanbul') { const path = tryResolve('@vitest/coverage-istanbul', [project.ctx.config.root]) if (path) { entries.push(path) + exclude.push('@vitest/coverage-istanbul') } } else if (provider === 'custom' && coverage.customProviderModule) { @@ -224,29 +249,6 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => { include.push(vue) } - const exclude = [ - 'vitest', - 'vitest/utils', - 'vitest/browser', - 'vitest/runners', - '@vitest/browser', - '@vitest/browser/client', - '@vitest/utils', - '@vitest/utils/source-map', - '@vitest/runner', - '@vitest/spy', - '@vitest/utils/error', - '@vitest/snapshot', - '@vitest/expect', - 'std-env', - 'tinybench', - 'tinyspy', - 'tinyrainbow', - 'pathe', - 'msw', - 'msw/browser', - ] - const svelte = tryResolve('vitest-browser-svelte', [project.ctx.config.root]) if (svelte) { exclude.push(svelte) From 5e3e1d2279ec4bd73442a41413a1b40c5399ff3a Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 3 Sep 2024 15:34:48 +0200 Subject: [PATCH 2/5] feat: add warning about optimized deps --- packages/browser/src/node/index.ts | 18 +++++++++++++++++- packages/vitest/src/public/node.ts | 11 ++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index 545e329e79ba..9db0225206b6 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -30,10 +30,26 @@ export async function createBrowserServer( const configPath = typeof configFile === 'string' ? configFile : false - const vite = await createServer({ + const logger = createViteLogger((process.env.VITEST_BROWSER_DEBUG as 'info') ?? 'info') + + const vite = await createViteServer({ ...project.options, // spread project config inlined in root workspace config base: '/', logLevel: (process.env.VITEST_BROWSER_DEBUG as 'info') ?? 'info', + customLogger: { + ...logger, + info(msg, options) { + logger.info(msg, options) + if (msg.includes('optimized dependencies changed. reloading')) { + logger.warn( + [ + c.yellow(`\n${c.bold('[vitest]')} ⚠️ Vite unexpectedly reloaded a test. This may cause tests to fail, lead to flaky behaviour or duplicated test runs.\n`), + c.yellow(`For a stable experience, please add mentioned dependencies to your config\'s ${c.bold('\`optimizeDeps.include\`')} field manually.\n\n`), + ].join(''), + ) + } + }, + }, mode: project.config.mode, configFile: configPath, // watch is handled by Vitest diff --git a/packages/vitest/src/public/node.ts b/packages/vitest/src/public/node.ts index 9e99c5296dac..5bf2ac58498a 100644 --- a/packages/vitest/src/public/node.ts +++ b/packages/vitest/src/public/node.ts @@ -1,3 +1,4 @@ +import { createServer as _createServer } from 'vite' import { TestModule as _TestFile } from '../node/reporters/reported-tasks' import type { ModuleDiagnostic as _FileDiagnostic } from '../node/reporters/reported-tasks' @@ -49,7 +50,15 @@ export type { JsonOptions } from '../node/reporters/json' export type { JUnitOptions } from '../node/reporters/junit' export type { HTMLOptions } from '../node/reporters/html' -export { isFileServingAllowed, createServer, parseAst, parseAstAsync } from 'vite' +export { + isFileServingAllowed, + parseAst, + parseAstAsync, + createLogger as createViteLogger, +} from 'vite' +/** @deprecated use `createViteServer` instead */ +export const createServer = _createServer +export const createViteServer = _createServer export type * as Vite from 'vite' export { TestCase, TestModule, TestSuite } from '../node/reporters/reported-tasks' From 02044ea9fad421488a19ff9c8c1269f7872f5a37 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 3 Sep 2024 15:36:24 +0200 Subject: [PATCH 3/5] chore: cleanup --- packages/browser/src/node/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index 9db0225206b6..1f34af84d99b 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -43,7 +43,7 @@ export async function createBrowserServer( if (msg.includes('optimized dependencies changed. reloading')) { logger.warn( [ - c.yellow(`\n${c.bold('[vitest]')} ⚠️ Vite unexpectedly reloaded a test. This may cause tests to fail, lead to flaky behaviour or duplicated test runs.\n`), + c.yellow(`\n${c.bold('[vitest]')} Vite unexpectedly reloaded a test. This may cause tests to fail, lead to flaky behaviour or duplicated test runs.\n`), c.yellow(`For a stable experience, please add mentioned dependencies to your config\'s ${c.bold('\`optimizeDeps.include\`')} field manually.\n\n`), ].join(''), ) From efe67abfa5dffc6731e8d981f52c1840059bef57 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 3 Sep 2024 15:51:42 +0200 Subject: [PATCH 4/5] chore: cleanup --- packages/browser/src/node/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index 1f34af84d99b..e2353006ba5c 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -1,6 +1,6 @@ import type { WorkspaceProject } from 'vitest/node' import type { Plugin } from 'vitest/config' -import { createServer } from 'vitest/node' +import { createViteLogger, createViteServer } from 'vitest/node' import c from 'tinyrainbow' import { version } from '../../package.json' import { setupBrowserRpc } from './rpc' From 9b779beeb2d2ddd6d63a65b77a11e0d30a6eda54 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 3 Sep 2024 16:01:55 +0200 Subject: [PATCH 5/5] chore: cleanup --- packages/browser/src/node/index.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index e2353006ba5c..5130cc4baaa6 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -30,12 +30,14 @@ export async function createBrowserServer( const configPath = typeof configFile === 'string' ? configFile : false - const logger = createViteLogger((process.env.VITEST_BROWSER_DEBUG as 'info') ?? 'info') + const logLevel = (process.env.VITEST_BROWSER_DEBUG as 'info') ?? 'info' + + const logger = createViteLogger(logLevel) const vite = await createViteServer({ ...project.options, // spread project config inlined in root workspace config base: '/', - logLevel: (process.env.VITEST_BROWSER_DEBUG as 'info') ?? 'info', + logLevel, customLogger: { ...logger, info(msg, options) {