Skip to content

Commit 316eb73

Browse files
authoredApr 22, 2024··
fix(vitest): fix false positive file filter match with leading slash (#5578)
1 parent 413ec5e commit 316eb73

File tree

8 files changed

+63
-12
lines changed

8 files changed

+63
-12
lines changed
 

‎packages/vitest/src/node/workspace.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,11 @@ export class WorkspaceProject {
284284
return testFiles.filter((t) => {
285285
const testFile = relative(dir, t).toLocaleLowerCase()
286286
return filters.some((f) => {
287-
const relativePath = f.endsWith('/') ? join(relative(dir, f), '/') : relative(dir, f)
288-
289287
// if filter is a full file path, we should include it if it's in the same folder
290-
if (isAbsolute(f)) {
291-
// the file is inside the filter path, so we should always include it,
292-
// we don't include ../file because this condition is always true if
293-
// the file doens't exist which cause false positives
294-
if (relativePath === '..' || relativePath === '../' || relativePath.startsWith('../..'))
295-
return true
296-
}
288+
if (isAbsolute(f) && t.startsWith(f))
289+
return true
297290

291+
const relativePath = f.endsWith('/') ? join(relative(dir, f), '/') : relative(dir, f)
298292
return testFile.includes(f.toLocaleLowerCase()) || testFile.includes(relativePath.toLocaleLowerCase())
299293
})
300294
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'vitest'
2+
3+
test('example', () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'vitest'
2+
3+
test('example', () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'vitest'
2+
3+
test('example', () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'vitest'
2+
3+
test('example', () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({})

‎test/filters/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "module",
44
"private": true,
55
"scripts": {
6-
"test": "vitest run"
6+
"test": "vitest"
77
},
88
"devDependencies": {
99
"vite": "latest",

‎test/filters/test/testname-pattern.test.ts

+44-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ import { expect, test } from 'vitest'
33

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

6-
test('match by partial pattern', async () => {
7-
const { stdout } = await runVitest({ root: './fixtures' }, ['example'])
6+
test.each([
7+
{ filter: 'example' },
8+
{ filter: '/example' },
9+
{ filter: resolve('./fixtures/test/example') },
10+
])('match by partial pattern $filter', async ({ filter }) => {
11+
const { stdout } = await runVitest({ root: './fixtures' }, [filter])
812

913
expect(stdout).toMatch('✓ test/example.test.ts > this will pass')
1014
expect(stdout).toMatch('Test Files 1 passed (1)')
@@ -42,3 +46,41 @@ test.each([
4246
expect(stdout).toMatch('× test/dont-run-this.test.ts > this will fail')
4347
expect(stdout).toMatch('✓ test/example.test.ts > this will pass')
4448
})
49+
50+
test.each([
51+
{
52+
filter: 'basic',
53+
files: [
54+
'test/basic.test.ts',
55+
'test/foo-basic/a.test.ts',
56+
'test/basic/a.test.ts',
57+
'test/basic-foo/a.test.ts',
58+
],
59+
},
60+
{
61+
filter: '/basic',
62+
files: [
63+
'test/basic.test.ts',
64+
'test/basic/a.test.ts',
65+
'test/basic-foo/a.test.ts',
66+
],
67+
},
68+
{
69+
filter: 'basic/',
70+
files: [
71+
'test/foo-basic/a.test.ts',
72+
'test/basic/a.test.ts',
73+
],
74+
},
75+
{
76+
filter: '/basic/',
77+
files: [
78+
'test/basic/a.test.ts',
79+
],
80+
},
81+
])('filter with slash $filter', async ({ filter, files }) => {
82+
const { stdout } = await runVitest({ root: './fixtures-slash' }, [filter])
83+
expect(stdout).toMatch(`Test Files ${files.length} passed (${files.length})`)
84+
for (const file of files)
85+
expect(stdout).toMatch(`✓ ${file}`)
86+
})

0 commit comments

Comments
 (0)
Please sign in to comment.