Skip to content

Commit

Permalink
fix(vitest): fix false positive file filter match with leading slash (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 22, 2024
1 parent 413ec5e commit 316eb73
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 12 deletions.
12 changes: 3 additions & 9 deletions packages/vitest/src/node/workspace.ts
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
@@ -0,0 +1,3 @@
import { test } from 'vitest'

test('example', () => {})
3 changes: 3 additions & 0 deletions test/filters/fixtures-slash/test/basic.test.ts
@@ -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
@@ -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
@@ -0,0 +1,3 @@
import { test } from 'vitest'

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

export default defineConfig({})
2 changes: 1 addition & 1 deletion test/filters/package.json
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
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}`)
})

0 comments on commit 316eb73

Please sign in to comment.