Skip to content

Commit

Permalink
fix(browser): fix testNamePattern config (#4909)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 9, 2024
1 parent 8e6c104 commit 4add951
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
13 changes: 13 additions & 0 deletions 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'
Expand Down Expand Up @@ -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 (_) {
Expand All @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions packages/vitest/src/api/setup.ts
Expand Up @@ -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'

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
}
}
27 changes: 27 additions & 0 deletions 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/)
})
7 changes: 5 additions & 2 deletions test/browser/vitest.config.mts
Expand Up @@ -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: {
Expand All @@ -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,
},
Expand Down

0 comments on commit 4add951

Please sign in to comment.