Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another way to allow leading space in testNamePattern #4119

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/runner/src/utils/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ export function interpretTaskModes(suite: Suite, namePattern?: string | RegExp,
}
}
if (t.type === 'test') {
if (namePattern && !getTaskFullName(t).match(namePattern))
t.mode = 'skip'
if (namePattern) {
const taskFullName = getTaskFullName(t)
// Match also the task full name with a leading space to be backward-compatible with tools like
// IntelliJ/WebStorm. Previous Vitest versions (<= 0.34.3) had the task full name starting
// with a space, and the tools passed `testNamePattern` matching it.
if (!(taskFullName.match(namePattern) || ` ${taskFullName}`.match(namePattern)))
t.mode = 'skip'
}
}
else if (t.type === 'suite') {
if (t.mode === 'skip')
Expand All @@ -46,7 +52,8 @@ export function interpretTaskModes(suite: Suite, namePattern?: string | RegExp,
}

function getTaskFullName(task: TaskBase): string {
return `${task.suite ? `${getTaskFullName(task.suite)} ` : ''}${task.name}`
const fullName = task.suite ? getTaskFullName(task.suite) : null
return fullName ? `${fullName} ${task.name}` : task.name
}

export function someTasksAreOnly(suite: Suite): boolean {
Expand Down
11 changes: 11 additions & 0 deletions test/filters/test/testname-pattern.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ test('match by pattern that also matches current working directory', async () =>
expect(stdout).toMatch('Test Files 1 passed (1)')
expect(stdout).not.toMatch('test/example.test.ts')
})

test('match by test name pattern with ^', async () => {
const { stdout } = await runVitest({
root: './fixtures',
testNamePattern: '^this',
}, ['filters'])

expect(stdout).toMatch('✓ test/filters.test.ts > this will pass')
expect(stdout).toMatch('Test Files 1 passed (1)')
expect(stdout).not.toMatch('test/example.test.ts')
})