diff --git a/packages/browser/src/client/main.ts b/packages/browser/src/client/main.ts index d0a9e029a352..b52032bddcf9 100644 --- a/packages/browser/src/client/main.ts +++ b/packages/browser/src/client/main.ts @@ -1,6 +1,7 @@ import { createClient } from '@vitest/ws-client' import type { ResolvedConfig } from 'vitest' import type { CancelReason, VitestRunner } from '@vitest/runner' +import { parseRegexp } from '@vitest/utils' import type { VitestExecutor } from '../../../vitest/src/runtime/execute' import { createBrowserRunner } from './runner' import { importId as _importId } from './utils' @@ -51,6 +52,7 @@ async function loadConfig() { try { await new Promise(resolve => setTimeout(resolve, 150)) config = await client.rpc.getConfig() + config = unwrapConfig(config) return } catch (_) { @@ -62,6 +64,17 @@ async function loadConfig() { throw new Error('cannot load configuration after 5 retries') } +function unwrapConfig(config: ResolvedConfig): ResolvedConfig { + return { + ...config, + // workaround RegExp serialization + testNamePattern: + config.testNamePattern + ? parseRegexp((config.testNamePattern as any as string)) + : undefined, + } +} + function on(event: string, listener: (...args: any[]) => void) { window.addEventListener(event, listener) return () => window.removeEventListener(event, listener) diff --git a/packages/vitest/src/api/setup.ts b/packages/vitest/src/api/setup.ts index 7f21120f7382..62529c426811 100644 --- a/packages/vitest/src/api/setup.ts +++ b/packages/vitest/src/api/setup.ts @@ -11,9 +11,9 @@ import type { ViteDevServer } from 'vite' import type { StackTraceParserOptions } from '@vitest/utils/source-map' import { API_PATH } from '../constants' import type { Vitest } from '../node' -import type { File, ModuleGraphData, Reporter, TaskResultPack, UserConsoleLog } from '../types' +import type { File, ModuleGraphData, Reporter, ResolvedConfig, TaskResultPack, UserConsoleLog } from '../types' import { getModuleGraph, isPrimitive, stringifyReplace } from '../utils' -import type { WorkspaceProject } from '../node/workspace' +import { WorkspaceProject } from '../node/workspace' import { parseErrorStacktrace } from '../utils/source-map' import type { TransformResultWithSource, WebSocketEvents, WebSocketHandlers } from './types' @@ -114,6 +114,9 @@ export function setup(vitestOrWorkspace: Vitest | WorkspaceProject, _server?: Vi await ctx.rerunFiles(files) }, getConfig() { + if (vitestOrWorkspace instanceof WorkspaceProject) + return wrapConfig(vitestOrWorkspace.getSerializableConfig()) + return vitestOrWorkspace.config }, async getTransformResult(id) { @@ -215,3 +218,14 @@ class WebSocketReporter implements Reporter { }) } } + +function wrapConfig(config: ResolvedConfig): ResolvedConfig { + return { + ...config, + // workaround RegExp serialization + testNamePattern: + config.testNamePattern + ? config.testNamePattern.toString() as any as RegExp + : undefined, + } +} diff --git a/test/browser/specs/filter.test.mjs b/test/browser/specs/filter.test.mjs new file mode 100644 index 000000000000..d5b421395083 --- /dev/null +++ b/test/browser/specs/filter.test.mjs @@ -0,0 +1,27 @@ +import assert from 'node:assert' +import test from 'node:test' +import { execa } from 'execa' + +test('filter', async () => { + const result = await execa( + 'npx', + [ + 'vitest', + 'run', + 'test/basic.test.ts', + '--testNamePattern', + 'basic 2', + '--browser.headless', + '--reporter=verbose', + ], + { + env: { + CI: '1', + NO_COLOR: '1', + }, + }, + ) + assert.match(result.stdout, /✓ test\/basic.test.ts > basic 2/) + assert.match(result.stdout, /Test Files {2}1 passed/) + assert.match(result.stdout, /Tests {2}1 passed | 3 skipped/) +}) diff --git a/test/browser/vitest.config.mts b/test/browser/vitest.config.mts index 0fcac15515be..87bc76d0eb45 100644 --- a/test/browser/vitest.config.mts +++ b/test/browser/vitest.config.mts @@ -6,6 +6,9 @@ const dir = dirname(fileURLToPath(import.meta.url)) function noop() {} +const provider = process.env.PROVIDER || 'webdriverio' +const browser = process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome') + export default defineConfig({ server: { headers: { @@ -19,9 +22,9 @@ export default defineConfig({ include: ['test/**.test.{ts,js}'], browser: { enabled: true, - name: process.env.BROWSER || 'chrome', + name: browser, headless: false, - provider: process.env.PROVIDER || 'webdriverio', + provider, isolate: false, slowHijackESM: true, },