Skip to content

Commit

Permalink
fix(vitest): correctly filter by parent folder
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 20, 2024
1 parent fee7d8b commit 49bb11a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ export class WorkspaceProject {
const testFile = relative(dir, t).toLocaleLowerCase()
return filters.some((f) => {
const relativePath = f.endsWith('/') ? join(relative(dir, f), '/') : relative(dir, 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

return testFile.includes(f.toLocaleLowerCase()) || testFile.includes(relativePath.toLocaleLowerCase())
})
})
Expand Down
14 changes: 13 additions & 1 deletion test/filters/test/testname-pattern.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'pathe'
import { join, resolve } from 'pathe'
import { expect, test } from 'vitest'

import { runVitest } from '../../test-utils'
Expand Down Expand Up @@ -30,3 +30,15 @@ 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.each([
['the parent of CWD', resolve(process.cwd(), '..')],
['the parent of CWD with slash', join(resolve(process.cwd(), '..'), '/')],
['the parent of a parent of CWD', resolve(process.cwd(), '..', '..')],
])('match by pattern that also matches %s: %s', async (_, filter) => {
const { stdout } = await runVitest({ root: './fixtures' }, [filter])

expect(stdout).toMatch('✓ test/filters.test.ts > this will pass')
expect(stdout).toMatch('× test/dont-run-this.test.ts > this will fail')
expect(stdout).toMatch('✓ test/example.test.ts > this will pass')
})

0 comments on commit 49bb11a

Please sign in to comment.