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

fix(vitest): fix false positive file filter match with leading slash #5578

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 3 additions & 9 deletions packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,11 @@ export class WorkspaceProject {
return testFiles.filter((t) => {
const testFile = relative(dir, t).toLocaleLowerCase()
return filters.some((f) => {
const relativePath = f.endsWith('/') ? join(relative(dir, f), '/') : relative(dir, f)

// if filter is a full file path, we should include it if it's in the same folder
if (isAbsolute(f)) {
// the file is inside the filter path, so we should always include it,
// we don't include ../file because this condition is always true if
// the file doens't exist which cause false positives
if (relativePath === '..' || relativePath === '../' || relativePath.startsWith('../..'))
return true
}
if (isAbsolute(f) && t.startsWith(f))
return true

const relativePath = f.endsWith('/') ? join(relative(dir, f), '/') : relative(dir, f)
return testFile.includes(f.toLocaleLowerCase()) || testFile.includes(relativePath.toLocaleLowerCase())
})
})
Expand Down
3 changes: 3 additions & 0 deletions test/filters/fixtures-slash/test/basic-foo/a.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest'

test('example', () => {})
3 changes: 3 additions & 0 deletions test/filters/fixtures-slash/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest'

test('example', () => {})
3 changes: 3 additions & 0 deletions test/filters/fixtures-slash/test/basic/a.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest'

test('example', () => {})
3 changes: 3 additions & 0 deletions test/filters/fixtures-slash/test/foo-basic/a.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest'

test('example', () => {})
3 changes: 3 additions & 0 deletions test/filters/fixtures-slash/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
2 changes: 1 addition & 1 deletion test/filters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"private": true,
"scripts": {
"test": "vitest run"
"test": "vitest"
},
"devDependencies": {
"vite": "latest",
Expand Down
46 changes: 44 additions & 2 deletions test/filters/test/testname-pattern.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { expect, test } from 'vitest'

import { runVitest } from '../../test-utils'

test('match by partial pattern', async () => {
const { stdout } = await runVitest({ root: './fixtures' }, ['example'])
test.each([
{ filter: 'example' },
{ filter: '/example' },
{ filter: resolve('./fixtures/test/example') },
])('match by partial pattern $filter', async ({ filter }) => {
const { stdout } = await runVitest({ root: './fixtures' }, [filter])

expect(stdout).toMatch('✓ test/example.test.ts > this will pass')
expect(stdout).toMatch('Test Files 1 passed (1)')
Expand Down Expand Up @@ -42,3 +46,41 @@ test.each([
expect(stdout).toMatch('× test/dont-run-this.test.ts > this will fail')
expect(stdout).toMatch('✓ test/example.test.ts > this will pass')
})

test.each([
{
filter: 'basic',
files: [
'test/basic.test.ts',
'test/foo-basic/a.test.ts',
'test/basic/a.test.ts',
'test/basic-foo/a.test.ts',
],
},
{
filter: '/basic',
files: [
'test/basic.test.ts',
'test/basic/a.test.ts',
'test/basic-foo/a.test.ts',
],
},
{
filter: 'basic/',
files: [
'test/foo-basic/a.test.ts',
'test/basic/a.test.ts',
],
},
{
filter: '/basic/',
files: [
'test/basic/a.test.ts',
],
},
])('filter with slash $filter', async ({ filter, files }) => {
const { stdout } = await runVitest({ root: './fixtures-slash' }, [filter])
expect(stdout).toMatch(`Test Files ${files.length} passed (${files.length})`)
for (const file of files)
expect(stdout).toMatch(`✓ ${file}`)
})